How to Redirect to Another Webpage using JavaScript?

There are three ways to redirect to another webpage 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.

window.location.replace("https://www.wplauncher.com");

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.

window.location.assign("https://www.wplauncher.com");

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 *