Javascript page Redirection

Maybe, you have seen while browsing internet this thing. When a form has been submitted successfully, in the webpage, there is some text like saying,
Update Successful, You will be redirected after 5 seconds. If you are not redirect automatically, please click in this link to redirect.
or, 
This site has been moved to a new domain, You will be redirected after 5 seconds.
The later message is shown when the website has been moved to a new domain name and you need to redirect the user, 
Now, how it's done
Here is the sample script to do this




<html>
<head>
<script type="text/javascript">
<!--
function delayer(){
window.location = "../javascriptredirect.php"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 5000)">

<h2>Prepare to be redirected!</h2>
<p>This page is a time delay redirect, please update your bookmarks to our new
location!</p>

</body>
</html>




Now, the explanation of this code fragment 
When the page is loaded, the onLoad event executes this script 

setTimeout('redirect()', 5000)
It sets a timeout after 5000 miliseconds / 5 seconds and after 5 seconds, the function redirect is called. In the function redirect, this window's location property is changed and this window is redirected to http://new-domain.com
The setTimeout() Method
Syntax var t=setTimeout("javascript statement",milliseconds);
The setTimeout() method returns a value - In the statement above, the value is stored in a variable called t. If you want to cancel this setTimeout(), you can refer to it using the variable name. The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('5 seconds!')" or a call to a function, like "redirect()". The second parameter indicates how many milliseconds from now you want to execute the first parameter. Note: There are 1000 milliseconds in one second.
Window Object  The window object represents an open window in a browser.
window.location  : Returns the Location object for the window 
 
 

0 comments :: Javascript page Redirection

Post a Comment