Timed Redirect Overview

The timed redirect is a useful construct, especially when moving a site from one domain to another. It allows use to display a notice on the first page telling your visitors where your site has moved to and then after they have had time to read the notice redirects them to that page. Other uses of javascript timers include implementing a delay before a download begins and refreshing your web page after a set time if the data on the page is changing rapidly (e.g. share prices).

Javascript Timed Redirect Code

In its simples form the timed redirect employs the built in javascript setTimeout command, for more complex timed redirects it is better to use a javascript function.

<script type="text/javascript">
<!--
setTimeout("window.location = 'https://www.example.com';",5000);
//-->
</script>

The above code redirects from the page it is on to www.example.com after a delay of 5000 milliseconds (5 seconds) - change the https://www.example.com to your destination URL and the 5000 to the number of milliseconds you want to delay for. To change seconds into milliseconds it is simply a case of multiplying by 1000.

An example of the time redirect can be seen by clicking the following link:

https://www.javascriptredirect.com/example-timed.htm

Using A Function In The Timed Redirect

In order to use a function to implement your timed redirect you will need to place the code for the function inside the HEAD tags and then the code that calls the function inside the BODY tags.

The code for the function is as follows:

<script type="text/javascript">
<!--
function mytimer(){
window.location = "https://www.example.com";
}
//-->
</script>

In the body you place the call to the function, this can be an onLoad on the BODY tag or an onClick on a button or on one of the many event handlers that can be implemented in a web page

<body onLoad="setTimeout('mytimer()',10000);">
<p>You will be redirected in 10 seconds</p>
</body>