JavaScript > Methods

Redirecting to other page in JavaScript

How to redirect the user from current page to another page using JavaScript?


If we want to redirect the user from current page to another page using JavaScript, we can follow this approach.

    <script>
        function TestFunction() {
            alert(location.href);
            location.href = "http://www.itfunda.com";
        }
    </script>

    <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />

In case we want to get the current url of the web page, we can get it using location.href as we have done in the alert method.

If we want to redirect the user from current page to another page, we can set the location.href property, in this case we have set to http://www.itfunda.com that redirects the user from current page to ITFunda.com website.

OUTPUT

In the output we can notice that first it will show alert as above and after clicking "OK" button in the alert the website ITFunda.com will open as below.

 Views: 10919 | Post Order: 54



Write for us






Hosting Recommendations