How to Returns the History object for the window (See History object) in JavaScript ?

In JavaScript, we can return the History object for the window using the window.history property. The History object represents the user's navigation history for the current session, including the URLs visited and the state of the page.

There are several methods and properties available in the History object that we can use to manipulate the browser history. Some of the commonly used methods are:

  • back(): This method loads the previous URL in the history list. It is equivalent to clicking the back button in the browser.


  • forward(): This method loads the next URL in the history list. It is equivalent to clicking the forward button in the browser.


  • go(): This method loads a specific URL from the history list. It takes an integer argument that represents the number of pages to go back or forward in the history list.


// Go back one page

// Go forward one page

// Go to the third page in the history list
  • pushState(): This method adds a new state to the history list. It takes three arguments: a state object, a title (which is currently ignored by most browsers), and a URL.


window.history.pushState({page: 1}, "Page 1", "/page1.html");
  • replaceState(): This method replaces the current state in the history list with a new state. It takes the same arguments as pushState().


window.history.replaceState({page: 2}, "Page 2", "/page2.html");

Note: The pushState() and replaceState() methods do not actually load the specified URL. They only add or replace a state in the history list. To load the URL, we need to use other methods such as location.assign() or location.replace().

About the author

William Pham is the Admin and primary author of With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.