Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Page Redirection in Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 277
    Comment on it

    What are the different ways to redirect page in javascript?

    Page Redirection is a situation when a  user locates from one page to another. There are two kinds of redirect :

     Server side redirect : In this,  server initiate redirection. 

     Client side redirect : In this,  client code does the redirection. 

    However, client side redirection is not preffered method for page redirection as a search engine may ignore JavaScript redirection.

    So, different methods used in JavaScript for page redirection according to your need are as follows :- 

    Case 1 : JavaScript Redirection to Another Page: All these following methods should be used in head section

    Using window.location method:  

    <script type="text/javascript">
    window.location="http://www.google.com";
    </script>

    .Using window.location.href method

    <script type="text/javascript">
    window.location.href="http://www.google.com";
    </script>

     Using window.location.assign method

    <script type="text/javascript">
    window.location.assign="http://www.google.com";
    </script


    Using window.location.replace method: 

    <script type="text/javascript">
    window.location.replace="http://www.google.com";
    </script>

    Case 2 : JavaScript Redirect to Another Page After 'y' Seconds

    Using onLoad method: Use this script in <body></body>

    <body onLoad="setTimeout(location.href='http://www.google.com', '4000')">

    Using Redirect function: Use this script in <head></head>

    <script type="text/javascript">
     
    function Redirect()
    {
     window.location="http://www.google.com";
    }
    document.write("You will be redirected to a new page in 4 seconds");
    setTimeout('Redirect()', 4000);
     
    </script>

    Case 3 : JavaScript Redirect to Another Page On Load

    Using onLoad method: Use this script in  <body></body>

    <body onLoad="setTimeout(location.href='http://www.google.com', '0')">

    Using a function: Use this script in <head> section.

    <script type="text/javascript">
     
    function Redirect()
    {
     window.location="http://www.google.com";
    }
    setTimeout('Redirect()', 0);
     
    </script>

    Case 4 : JavaScript Redirect to Another Page On Click

     Using a function: Use this  scripts in the head and body section to redirect to another page on button click.

    <head>
    <script>
    function myButton()
    {
    window.location="http://www.google.com";
    }
    </script>
    </head>
    <body>
    <button onclick="myButton()">Click me</button>
    </body>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: