W3cubDocs

/Web APIs

History API

The History API provides access to the browser's session history (not to be confused with WebExtensions history) through the history global 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.

Note: This API is only available on the main thread (Window). It cannot be accessed in Worker or Worklet contexts.

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:

js

history.back();

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:

js

history.forward();

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()):

js

history.go(-1);

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

js

history.go(1);

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

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

js

// The following statements
// both have the effect of
// refreshing the page
history.go(0);
history.go();

You can determine the number of pages in the history stack by looking at the value of the length property:

js

const numberOfEntries = history.length;

Interfaces

History

Allows manipulation of the browser session history (that is, the pages visited in the tab or frame that the current page is loaded in).

PopStateEvent

The interface of the popstate event.

Examples

The following example assigns a listener for the popstate event. It then illustrates some of the methods of the history object to add, replace, and move within the browser history for the current tab.

js

window.addEventListener("popstate", (event) => {
  alert(
    `location: ${document.location}, state: ${JSON.stringify(event.state)}`,
  );
});

history.pushState({ page: 1 }, "title 1", "?page=1");
history.pushState({ page: 2 }, "title 2", "?page=2");
history.replaceState({ page: 3 }, "title 3", "?page=3");
history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null"
history.go(2); // alerts "location: http://example.com/example.html?page=3, state: {"page":3}"

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
History_API 1 12 1 10 3 1 4.4 18 4 10.1 1 1.0
back 1 12 1 10 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
forward 1 12 1 10 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
go 1 12 1 10 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
length 1 12 1 10 ≤12.1 1 4.4 18 4 ≤12.1 1 1.0
pushState 5 12
4Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
10 11.5 5 ≤37 18
4Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
11.5 4 1.0
replaceState 5 12
4Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
10 11.5 5 ≤37 18
4Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
11.5 4 1.0
scrollRestoration 46 79 46 No 33 11 46 46 46 33 11 5.0
state 19 12 4 10 ≤12.1 6 4.4 25 4 ≤12.1 6 1.5

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/History_API