JAVASCRIPT Tutorial

JS Location

Concept: Location Object

The location object in JavaScript provides access to the current web page's URL and navigation-related methods.

Steps:

URL Manipulation
  • Getting the Current URL: Use location.href to retrieve the full URL of the current page.
  • Modifying the URL: Assign a new value to location.href to change the URL.
  • Setting Hash: Use location.hash to set or retrieve the fragment identifier in the URL.
Page Reloading
  • Reload Current Page: Call location.reload() to reload the current page.
  • Reload with No Cache: Use location.reload(true) to reload without using the browser's cache.
URL Redirection
  • Immediate Redirection: Redirect the browser to a new URL using location.replace().
  • Delayed Redirection: Use window.location.assign() for a delayed redirection.
Navigation
  • Forward/Backward Navigation: Use history.forward() and history.back() to navigate forward or backward in browsing history.
  • Changing Document Title: Set document.title to change the title of the current page.

Example:

// Get current URL
const currentURL = location.href;

// Change URL to another page
location.href = "https://example.com";

// Reload page without cache
location.reload(true);

// Redirect to a new URL
location.replace("https://newsite.com");

This script demonstrates getting the current URL, changing the URL to a different page, reloading the page without cache, and redirecting to a new URL.