How do I redirect to another webpage?

In this article, learn how to redirect to another webpage with code examples and a demo.

How do I redirect to another webpage?
How do I redirect to another webpage?

To redirect to another webpage using JavaScript, you can use the window.location object to set the URL of the new page.

Here’s an example:

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

This will redirect the user to the URL specified in the href attribute of the window.location object.

You can also use the window.location.replace() method to redirect to a new page, which replaces the current page in the browser history.

So the user can’t use the “back” button to navigate back to the original page.

Here’s an example:

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

This method is useful if you don’t want the user to be able to go back to the previous page after the redirection.

Note that in some cases, the browser may block automatic redirects or show a warning message to the user before proceeding.

Make sure to inform the user that they are being redirected and give them the option to cancel the operation if necessary.

CodePen Source Code: https://codepen.io/arctutorials/pen/ExeJpdy

See also  Mutation Testing Complete Tutorial