Web | This Site

JavaScript Basics
JavaScript Objects
JavaScript Advanced
JS Useful References
JS Useful References
JS Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# JavaScript - Page Redirection

## What is page redirection ?

When you click a URL to reach to a page X but internally you are directed to another page Y that simply happens because of page re-direction. This concept is different from JavaScript Page Refresh.

There could be various reasons why you would like to redirect from original page. I'm listing down few of the reasons:

- You did not like the name of your domain and you are moving to a new one. Same time you want to direct your all visitors to new site. In such case you can maintain your old domain but put a single page with a page re-direction so that your all old domain visitors can come to your new domain.

- You have build-up various pages based on browser versions or their names or may be based on different countries, then instead of using your server side page redirection you can use client side page redirection to land your users on appropriate page.

- The Search Engines may have already indexed your pages. But while moving to another domain then you would not like to lose your visitors coming through search engines. So you can use client side page redirection. But keep in mind this should not be done to make search engine a fool otherwise this could get your web site banned.

## How Page Re-direction works ?

## Example 1:

This is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows:

    <head>
    <script type="text/javascript">
    <!--
       window.location="http://www.newlocation.com";
    //-->
    </script>
    </head>

To understand it in better way you can Try it yourself.

## Example 2:

You can show an appropriate message to your site visitors before redirecting them to a new page. This would need a bit time delay to load a new page. Following is the simple example to implement the same:

    <head>
    <script type="text/javascript">
    <!--
    function Redirect()
    {
        window.location="http://www.newlocation.com";
    }

    document.write("You will be redirected to main page in 10 sec.");
    setTimeout('Redirect()', 10000);
    //-->
    </script>
    </head>

Here setTimeout() is a built-in JavaScript function which can be used to execute another function after a given time interval.

To understand it in better way you can Try it yourself.

## Example 3:

Following is the example to redirect site visitors on different pages based on their browsers :

    <head>
    <script type="text/javascript">
    <!--
    var browsername=navigator.appName;
    if( browsername == "Netscape" )
    {
       window.location="http://www.location.com/ns.htm";
    }
    else if ( browsername =="Microsoft Internet Explorer")
    {
       window.location="http://www.location.com/ie.htm";
    }
    else
    {
      window.location="http://www.location.com/other.htm";
    }
    //-->
    </script>
    </head>

Advertisement

Online Image Processing

Indian Baby Names