Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to redirect a web page with JavaScript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 243
    Comment on it

    Hello Readers,
    There are many ways to redirect a web page to another web page. We can do this via server redirects and JavaScript redirects. But the question is which method is best and appropriate for redirection as there are many methods available. In this article we will demonstrate what method we should use.


    There are several number of methods to redirect a webpage through JavaScript are as follows:

    • Window.location : It sets the new location of the current window.
    • window.location.href : It sets the new URL for the current window.
    • window.location.assign : It assigns the new URL to current window.
    • window.location.replace : It replaces the current location with new location.
    • self.location : It sets the location of current window itself.
    • top.location : It sets the location of the top most window of current window.

    They all work in similar manner in terms of redirection but slightly different. When you call top.location for redirection in a iframe then the main window will be redirected.

    Another method is location.replace will replace the current location with new one so we can't get the old one with back button of the browser.

    The better alternative for redirection is window.location.href . When you want to redirect a webpage on Page Load. When you want to redirect a user to another website after your website opened you need to call window.location.href in the top of the page i.e within head tag.

     

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

    You can redirect to a page within a time period and after an event or user action. For Example:

     

    • Redirection within given time: We can redirect to one page to another within a time period.
    setTimeout(function() {
      window.location.href = "http://www.google.com";
    }, 3000);

     

    • Redirection after an event or user action:
    document.getElementById("button").onclick = function() {
      window.location.href = "http://www.google.com";
    };

    In the above code when user click on a button the page will redirect to google.com.
     

    if (a == b) {
      window.location.href = "http://www.google.com";
    }

    In the above code if the condition is true then page will redirect to google.com .

    This article is about how JavaScript redirect works. Hope this will help you :)

 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: