X



Home>Tutorial>

What is JavaScript Location Object

Nuelson Penuel Saturday, July 22, 2023 Javascript

The JavaScript Location object represents the current URL of the browser window and provides various properties and methods to manipulate and retrieve information about the URL. It allows developers to perform operations, such as changing the URL, navigating to different pages, extracting information from the URL, and more. Below are some examples of using the Location object: 1. Accessing URL properties: The Location object provides several properties to retrieve information about the URL. For example:
   console.log(location.href); // Retrieves the complete URL
   console.log(location.protocol); // Retrieves the protocol (e.g., "https:")
   console.log(location.host); // Retrieves the host and port of the URL (e.g., "www.example.com:8080")
   console.log(location.hostname); // Retrieves the hostname of the URL (e.g., "www.example.com")
   console.log(location.pathname); // Retrieves the path of the URL (e.g., "/folder/page.html")
   console.log(location.search); // Retrieves the query string of the URL (e.g., "?param=value")
   console.log(location.hash); // Retrieves the hash portion of the URL (e.g., "#section")
   
2. Navigating to a different URL: The Location object allows developers to navigate to a different URL. For example:
   location.href = "https://www.example.com"; // Redirects to a different URL
   location.assign("https://www.example.com"); // Same as location.href
   location.replace("https://www.example.com"); // Replaces the current page without adding to the browser history
   location.reload(); // Reloads the current page
   
3. Redirecting based on conditions: Developers can use the Location object to redirect users based on specific conditions. For example:
   if (location.hostname !== "www.example.com") {
     location.href = "https://www.example.com";
   }
   
4. Extracting URL parameters: The Location object provides the `searchParams` property, which allows developers to access and manipulate the query string parameters. For example:
   // URL: https://www.example.com/?name=John&age=30
   const params = new URLSearchParams(location.search);
   console.log(params.get("name")); // Retrieves the value of the "name" parameter ("John")
   console.log(params.get("age")); // Retrieves the value of the "age" parameter ("30")
   
5. Modifying the hash part of the URL: The Location object can be used to update the hash portion of the URL, which is often used for page scrolling or creating dynamic links within a page. For example:
   location.hash = "#section"; // Adds the specified hash to the URL
   
6. Refreshing the page after a specific time: Developers can use the Location object with the setTimeout function to refresh the page after a specific time interval. For example:
   setTimeout(function() {
     location.reload();
   }, 5000); // Reloads the page after 5 seconds
   
The JavaScript Location object provides a wide range of properties and methods for manipulating and retrieving information about the URL. It is essential for tasks such as URL navigation, extracting parameters, modifying the URL itself, and more.

| | Views(149)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services