javascript location.href

What is javascript location.href?

JavaScript is a popular programming language used to create interactive websites. One of the most commonly used objects in JavaScript is the "location" object, which provides information about the web page's location and allows you to manipulate it. The "location.href" property is used to get or set the URL of the current web page.

Using location.href to redirect the page

One of the most common uses of the "location.href" property is to redirect the user to a different web page. This can be done by setting the "location.href" property to the URL of the new page:


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

This will immediately redirect the user to the specified URL.

Using location.href to reload the page

You can also use the "location.href" property to reload the current web page. This can be done by setting "location.href" to the current URL:


location.href = location.href;

This will cause the web page to reload, essentially refreshing it.

Using location.href with window.open()

The "location.href" property can also be combined with the "window.open()" method to open a new window or tab with a specified URL:


window.open(location.href, "_blank");

This will open a new window or tab with the same URL as the current web page.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe