X



Home>Tutorial>

Understanding JavaScript History Object

Nuelson Penuel Saturday, July 22, 2023 Javascript

The JavaScript History object represents the browsing history of the current tab or window. It provides methods and properties to navigate through the browser's history. Developers can use the History object to move forward or backward between visited pages, manipulate the URL without navigating, and retrieve information about the browsing history. Below are examples of using the History object: 1. Moving back and forward in history: The History object provides methods to move backward or forward in the browsing history. For example:
   history.back(); // Moves back to the previous page in history
   history.forward(); // Moves forward to the next page in history
   history.go(-2); // Moves back two pages in history
  
2. Manipulating history without navigation: The History object allows manipulation of the URL without navigating to a new page. For example:
   history.pushState(null, "", "/new-page"); // Adds a new entry to the history with a new URL
   history.replaceState(null, "", "/updated-page"); // Replaces the current entry in the history with a new URL
   
3. Retrieving information about the history: The History object provides properties to access information about the browsing history. For example:
   console.log(history.length); // Retrieves the number of entries in the history
   console.log(history.state); // Retrieves the state object associated with the current URL
   
4. Navigating to a specific page in history: The History object allows developers to navigate to a specific page in the browsing history. For example:
   history.go(2); // Moves forward two pages in history
   history.go(-1); // Moves back one page in history
   
5. Listening for history changes: Developers can use the onpopstate event to listen for changes in the browsing history. For example:
   window.onpopstate = function(event) {
     console.log("History change detected");
     console.log(event.state); // Retrieves the state object associated with the new URL
   };
   
6. Using history with replaceState: The History object can be used in combination with replaceState to modify the URL without triggering a new page load. For example:
   function updateURL() {
     history.replaceState(null, "", "/updated-page");
   }
   updateURL(); // Replaces the current URL with "/updated-page" without reloading the page

   function getUrl() {
     console.log(location.href);
   }
   getUrl(); // Prints the current URL: "https://www.example.com/updated-page"
   
The JavaScript History object enables developers to navigate through the browser's history, manipulate URLs without navigation, retrieve information about the history, and listen for history changes. It is useful for building dynamic applications that require control over the browsing history.

| | Views(169)

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