JavaScript Page Redirection

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.

JavaScript Page Redirection Example

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://jobails.com/java";
      }
      else if(borwser_name == "Netscape")
      { 
         window.location="http://jobails.com/c";
      }
      else if(borwser_name =="Microsoft Internet Explorer")
      {
         window.location="http://jobails.com/cpp";
      }
      else
      {
         window.location="http://jobails.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:

javascript page redirection

Now click on the Click to Activate Page Redirection button. Here is the output produced in Safari browser:

page redirection using javascript

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.

JavaScript Online Test


« Previous Tutorial Next Tutorial »