Redirect to Another page using JavaScript

There are three ways to redirect to another page using JavaScript.

Option 1: Use window.location.replace

The JavaScript code below will redirect the user to the URL specified. According to Mozilla, the replace method “replaces the current resource with the one at the provided URL” (Source). Replace removes the current document’s URL from the user’s history, so that it’s not possible to use the back button.


Option 2: Use window.location.assign

While the assign method will also redirect the user to the specified URL, it will allow a user to go back to the original document that you redirected from.


Option 3: Use window.location.href

The following JavaScript code acts more like a click compared to replace and assign which act more like HTTP redirects. When you add the following code to your JavaScript file, you will redirect the user to the specified webpage.

window.location.href = "https://www.wplauncher.com";

Leave a Reply

Your email address will not be published. Required fields are marked *