window.location.href: Complete Usage Guide

The window.location.href is used to retrieve or set the URL address of the current page.

  1. Get the URL of the current page:
    var url = window.location.href;
  2. Set the URL of the current page to: window.location.href = “https://www.example.com”;
  3. Redirect to another page:
    window.location.href = “https://www.example.com/page2”;
  4. Open a new URL in the current page: window.open(“https://www.example.com”);
  5. Refresh the current page:
    window.location.href = window.location.href;
  6. Obtain the query parameters from the URL by storing them in the variable “queryString” using window.location.search.
  7. 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.

bannerAds