Publishing System Settings Logout Login Register
Redirect your browser after a set amount of time
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on April 12th, 2007
7966 views
JavaScript
This tutorial is intended for Intermediate JavaScript users, basic knowledge of variables required.
This is the first of five JavaScript redirection tutorials I will be writing.

The files used in this tutorial are available for download. (.zip)


Table of Contents
1 - Introduction
2 - The Variables and Function
3 - The Page & Conclusion

- Introduction
After reading and completing this tutorial you will have learned how to make your Browser redirect to a different page after a set amount of time, using variables and functions. We will also make a basic HTML page for our redirection.

Lets get started.



- The Variables and Function
The first thing we are going to do is make our Variables. Variables are commands that tell something what to do when they are called upon; we will be using two of them to set the amount of time we will wait before redirecting, and to set the location of our redirection.

Lets make our variables, we will title them redirTime for the time limit, and redirURL for the location.
redirTime = "3000";
redirURL = "http://www.yoursite.com";

redirTime tells the site how long to wait before redirecting, redirURL tells the site the location you want to redirect to in this case replace http://www.yoursite.com with your site address.

Now for our function it will be called redirTimer
function redirTimer() {
 self.setTimeout("self.location.href = redirURL;",redirTime);
}

Our Function redirTimer tells the browser the location of our redirURL variable and the time of our redirTime variable, and then redirects after the time has passed.

The final code for our variables and functions.
redirTime = "3000"; //Sets the time for our timer
redirURL = "http://www.yoursite.com"; //Sets the location that we want to redirect to

function redirTimer() {//Begins our function
 self.setTimeout("self.location.href = redirURL;",redirTime);//Tells the browser to redirect to our site after the set amount of time.
}//Closes our function




- The Page
Now that we have our Variables and Functions lets include them into a simple HTML page.

The HTML page
<html>
<head>
 <script language="javascript">
 redirTime = "3000";
 redirURL = "http://www.yoursite.com";
 function redirTimer() {
  self.setTimeout("self.location.href = redirURL;",redirTime);
 }
</script>
<title>Our Redirection</title>
</head>
<body onload="redirTimer()">

</body>
</html>


You can also include the JavaScript from an external file by using <script type="text/javascript" src="myjavascript.js" /> Both ways are included in the tutorial files download.


- Conclusion
You now know how to redirect a browser after a set amount of time, you also became more familiar with Functions and Variables and how they work.

The files used in this tutorial our available  for download. (.zip)
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
David Knight

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top