Page redirection helps to send user to page B instead of page A. In other words, page redirection in JavaScript means that when a user clicks on a URL to go to the page A, but internally you are directed the user to another page B.
Here is an example of page redirection using JavaScript.
<html> <head> <title>JavaScript Page Redirection</title> <script type="text/javascript"> function pageRedirect() { var borwser_name = navigator.appName; if(borwser_name == "Google Chrome") { window.location="http://codescracker.com/java"; } else if(borwser_name == "Netscape") { window.location="http://codescracker.com/c"; } else if(borwser_name =="Microsoft Internet Explorer") { window.location="http://codescracker.com/cpp"; } else { window.location="http://codescracker.com"; } } </script> </head> <body> <input type="submit" value="Click to Activate Page Redirection" onclick="pageRedirect()"> </body> </html>
Here are some sample outputs produced by the above page redirection example program using JavaScript. This is the initial output:
Now click on the Click to Activate Page Redirection button. Here is the output produced in Safari browser:
You can also use setTimeout() function to redirect to another page in given time. To use setTimeout() function in page redirection using JavaScript, here is the general form:
setTimeout('Redirect()', 5000);
The function Redirect() in setTimeout(), triggers after 5 seconds.