How to Redirect a Web Page in Javascript
JavaScript is used to execute actions, process form data and create alerts or pop-up windows on a web page. You can use JavaScript in conjunction with hypertext markup language (HTML) code to create a redirect. A redirect is similar to forwarding mail to another address. Visitors to your website will be automatically redirected to a different web page, to keep you from losing clients. The redirection can be set to load after a set number of seconds.
Instructions
-
-
1
Open a text editor of your choice to create a HTML page.
-
2
Add the following code to the page, but replace "http://www.new-website-location.com" with your current website address:
<html>
<body>
<script type="text/javascript">
<!--
window.location = "http://www.new-website-location.com/"
//-->
</script>
</body>
</html> -
-
3
Create a code that redirects visitors to a website after seven seconds by using the following code (originally derived from Tizag) instead:
<html>
<head>
<script type="text/javascript">
<!--
function redirect(){
window.location = "http://www.new-website-location.com/"
}
//-->
</script>
</head><body onLoad="setTimeout('redirect()', 7000)">
</body>
</html> -
4
Replace the website location and "7000" with the actual web address and number of seconds you want to use.
-
5
Save the page by clicking on "File" and "Save." Name the page "index.html" to redirect visitors from your home page to the new address. Otherwise, use the name of the HTML page you are redirecting visitors from. Upload the HTML file to the Internet.
-
1
Tips & Warnings
You can include text that notifies visitors of your new website address, provides the number of seconds before a redirect or links directly to the new address.
References
- Photo Credit internet image by Soja Andrzej from Fotolia.com