The DOM Window
object provides access to the browser's session history (not to be confused for [[../../../Mozilla/Add-ons/WebExtensions/API/history|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:
window.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:
window.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()
):
window.history.go(-1)
To move forward a page, just like calling forward()
:
window.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:
// The following statements
// both have the effect of
// refreshing the page
window.history.go(0)
window.history.go()
You can determine the number of pages in the history stack by looking at the value of the length
property:
let numberOfEntries = window.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).
Examples
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.
window.onpopstate = function(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 | Status | Comment |
---|---|---|
HTML Living StandardThe definition of 'History' in that specification. | Living Standard | No change from HTML5. |
HTML5The definition of 'History' in that specification. | Recommendation | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
History
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 10 |
Opera
Full support 3 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
back
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
forward
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
go
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
length
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
pushState
|
Chrome
Full support 5 |
Edge
Full support 12 |
Firefox Full support 4 Full support 4 Notes' Until 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. |
IE
Full support 10 |
Opera
Full support 11.5 |
Safari
Full support 5 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android Full support 4 Full support 4 Notes' Until 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. |
Opera Android
Full support 11.5 |
Safari iOS
Full support 4.3 |
Samsung Internet Android
Full support 1.0 |
replaceState
|
Chrome
Full support 5 |
Edge
Full support 12 |
Firefox Full support 4 Full support 4 Notes' Until 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. |
IE
Full support 10 |
Opera
Full support 11.5 |
Safari
Full support 5 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android Full support 4 Full support 4 Notes' Until 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. |
Opera Android
Full support 11.5 |
Safari iOS
Full support 4.3 |
Samsung Internet Android
Full support 1.0 |
scrollRestoration
|
Chrome
Full support 46 |
Edge
Full support 79 |
Firefox
Full support 46 |
IE
No support No |
Opera
Full support 33 |
Safari
Full support Yes |
WebView Android
No support No |
Chrome Android
Full support 46 |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support 5.0 |
state
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.'
- See implementation notes.
See also
References
Guides
History API by Mozilla Contributors is licensed under CC-BY-SA 2.5.