JavaScript Redirect
When moving to a new domain name, JavaScript redirects can be highly useful, especially if you have a time-delay placeholder on your download site or a list of external web servers that mirror your content.
To inform your visitors about the move, you can create a "redirect page" at the old location. This page can automatically forward visitors to the new site after a brief delay, effectively notifying them of the change. JavaScript redirection is an excellent tool for implementing this kind of transition.
JavaScript Window.Location
Control over which page loads in the browser is managed through the JavaScript property
window.location
. By assigning a new URL to
window.location
, you can redirect the current webpage to the specified one. For example, if you wanted to redirect all your visitors to
www.google.com upon arriving at your site, you would simply use the following script:
HTML & JavaScript Code:
<script type="text/javascript">
window.location = "http://www.google.com/"
</script>
JavaScript Time Delay
Implementing a timed delay in JavaScript can be beneficial in several scenarios, such as:
- Displaying an "Update your bookmark" page when you need to change URLs or page locations.
- Introducing a timed delay before a download begins on download sites.
- Refreshing a webpage at specified intervals.
While the code for setting up a timed delay is somewhat complex and beyond the scope of this tutorial, we've tested it, and it works reliably.
HTML & JavaScript Code:
<html>
<head>
<script type="text/javascript">
function delayerFun(){
window.location = "../page_name.html"
}
</script>
</head>
<body onLoad="setTimeout('delayerFun()', 3000)">
<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 to be used after 5 seconds or 5000 milliseconds (5 seconds), so we pass the
setTimeout() two arguments.
- 'delayerFun()' - The function we want setTimeout() to execute after the specified delay.
- 3000 - the number of millisecods we want setTimeout() to wait before executing our function. 1000 miliseconds = 1 second.