History Web API


The DOM Window object provides access to the browser’s session history (not to be confused for WebExtensions history) through the history object.

It exposes useful methods and properties that let you navigate back and forth through the user’s history, and manipulate the contents of the history stack.

Concepts and usage

Moving backward and forward through the user’s history is done using the back(), forward(), and go() methods.

Moving forward and backward

To move backward through history:


This acts exactly as if the user clicked on the Back button in their browser toolbar.

Similarly, you can move forward (as if the user clicked the Forward button), like this:


Moving to a specific point in history

You can use the go() method to load a specific page from session history, identified by its relative position to the current page. (The current page’s relative position is 0.)

To move back one page (the equivalent of calling back()):


To move forward a page, just like calling forward():


Similarly, you can move forward 2 pages by passing 2, and so forth.

Refresh the current page

Another use for the go() method is to refresh the current page by either passing 0, or by invoking it without an argument:

// The following statements
// both have the effect of
// refreshing the page


The following example assigns a listener to the onpopstate property.

And then illustrates some of the methods of the history object to add, replace, and move within the browser history for the current tab

 1 window.onpopstate = function(event) {
 2   alert(`location: ${document.location}, state: ${JSON.stringify(event.state)}`)
 3 }
 5 history.pushState({page: 1}, "title 1", "?page=1")
 6 history.pushState({page: 2}, "title 2", "?page=2")
 7 history.replaceState({page: 3}, "title 3", "?page=3")
 8 history.back() // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
 9 history.back() // alerts "location: http://example.com/example.html, state: null"
10 history.go(2)  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}"