Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. History API

History API

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheHistory API provides access to the browser's session history (not to be confused withWebExtensions history) through thehistory 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 inWorker orWorklet contexts.

Concepts and usage

Moving backward and forward through the user's history is done using theback(),forward(), andgo() methods.

Moving forward and backward

To move backward through history:

js
history.back();

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

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

js
history.forward();

Moving to a specific point in history

You can use thego() method to load a specific page from session history, identified by its relative position to the current page. (The current page's relative position is0.)

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

js
history.go(-1);

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

js
history.go(1);

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

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

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

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

js
const numberOfEntries = history.length;

Interfaces

History

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

PopStateEvent

The interface of thepopstate event.

Examples

The following example assigns a listener for thepopstate 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

Specification
HTML
# the-history-interface

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp