JAVASCRIPT » Redirection

  Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

Redirect a web browser Using JavaScript


When a web page is moved will most probably would like to place a "redirect" page at the old location that informs the visitor of the change and then after a timed delay, will forward the visitor to the new location. With the javascript redirection, you can do just this.

HTML & Javascript Code:

<script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//-->
</script>

Javascript Time Delay

Implementing a timed delay in javascript is useful for the following situations:

Showing an "Update your bookmark" page when you have to change URLs or page locations

For download sites that wish to have a few second delay before the page loads and when the download starts

To refresh a web page every specified amount of seconds

The code for this timed delay is slightly involved and is beyond the scope of this tutorial. However, we have tested it and it seems to function properly.

HTML & Javascript Code:

<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>

View Page:

The most important part of getting the delay to work is being sure to use the javascript function setTimeout. We want the delayer() function be used after 5 seconds or 5000 miliseconds, so we pass the setTimeout() two arguments.

  • 'delayer()' - The function we want setTimeout() to execute after the specified delay.
  • 5000 - the number of milisecods we want setTimeout() to wait before executing our function. 1000 miliseconds = 1 second.

Web Page Redirection

Do use javascript for redirections when you change your website's URL or move a file to a new location. Don't use redirections when they could easily be replaced with a normal HTML hyperlink.


Free   Version: n/a   Platform(s): All   Updated:  July 20, 2008

Developer:EPSYLONSYS Demo Download  
Rated by: 1 user(s)  
Follow Navioo On Twitter

Submit a resource