JavaScript Page direct

The window.location object in JavaScript can be used to send the viewer to a separate web website.

Example:

window. location.href = "http://www.twitter.com";

In this example, you will set the href property of the window. location object to a new URL (http://www.twitter.com), which causes the browser to navigate to that URL.

Note that you can also use other properties of the window. location object to manipulate the URL in different ways, such as changing the hostname, pathname, or search parameters.

You can also use the location.replace() method to redirect the user to a new page, but with the additional feature of replacing the current page in the browser history, which means that the user can't navigate back to the previous page using the Back button. Here's an example:

window. location.replace("http://www.twitter.com");

In this example, you will call the replace() method of the window. location object with the new URL (http://www.twitter.com) as the argument, which causes the browser to replace the current page with the new page, effectively removing the current page from the browser history.

Note that when redirecting a user to a new page, you should always ensure that the new page is appropriate and relevant to the user's intended destination. Additionally, you should avoid using redirect loops, which can cause the browser to get stuck in an infinite loop of redirecting between pages.

Last updated