Home > DeveloperSection > Articles > JavaScript Page Redirection

JavaScript Page Redirection


JavaScript JavaScript 
Ratings:
0 Comment(s)
 2646  View(s)
Rate this:

JavaScript Page Redirection

Introduction:

In this article I am going to show how to redirect user to a page using Java Script, there is a function in Java Script which is used for the redirection purpose. Some time redirection is used to take users to a page depending on the their browser’s version and name.

Window.location : It is used to transfer the page to the specified url.

If the function is placed in the head section it is transferred as soon as the page is loaded, but when we call a function on a button it will be called when the user clicks on the button.

 

<html>

<head>

    <script type="text/javascript">

<!--

        function PageRedirect() {

            window.location = "http://www.google.com";

        }

-->

    </script>

</head>

<body>

    <p> If user clicks on the button page will be redirected</p>

    <form>

    <div>

        <input type="button" value="Redirect Me" onclick="PageRedirect();" />

    </div>

    </form>

</body>

</html>

 

Output:

JavaScript Page Redirection

As soon as the user clicks on button then he will be redirected to the google page.

 

If we want the page to be transferred as soon as the page loads we will write the code at the head section and do not call at the button click like the example given below:

 

 

<html>

<head>

    <script type="text/javascript">

        window.location = "http://www.google.com";

    </script>

</head>

<body>

    <p>

        As soon as the browsers loads it will be redirected to www.google.com</p>

</body>

</html>

 

Output of the above code:

JavaScript Page Redirection

 

We can also use setTimeout function to transfer the page after some time (5 sec) has elapsed:

 

<html>

<head>

    <script type="text/javascript">

        function Redirect() {

            window.location = "http://www.google.com";

        }

        document.write("You will be transferred to page after 5 sec.");

        setTimeout('Redirect()', 5000);

    </script>

</head>

<body>

</body>

</html>

 

Conclusion:

At last I conclude, from this article you can know the use of Java Script Page Redirect method how to use it and how to transfer one page from the other page using window.location function.


Don't want to miss updates? Please click the below button!

Follow MindStick