window.location.href: Complete Usage Guide
The window.location.href is used to retrieve or set the URL address of the current page.
- Get the URL of the current page:
var url = window.location.href; - Set the URL of the current page to: window.location.href = “https://www.example.com”;
- Redirect to another page:
window.location.href = “https://www.example.com/page2”; - Open a new URL in the current page: window.open(“https://www.example.com”);
- Refresh the current page:
window.location.href = window.location.href; - Obtain the query parameters from the URL by storing them in the variable “queryString” using window.location.search.
- Determine if the current page is using the https protocol:
var isHttps = (window.location.protocol === “https:”);
Summary: window.location.href is a frequently used property for controlling and obtaining URL addresses, allowing for operations such as redirecting pages, refreshing pages, and fetching query parameters from the URL.