History

History interface allows manipulation of the browser session history , that is the pages visited in the tab or frame that the current page is loaded in.

特性

History interface doesn't inherit any property.

length 只读
Returns an Integer representing the number of elements in the session history, including the currently loaded page. For example, for a page loaded in a new tab this property returns 1 .
scrollRestoration
Allows web applications to explicitly set default scroll restoration behavior on history navigation. This property can be either auto or manual .
state 只读
Returns an any value representing the state at the top of the history stack. This is a way to look at the state without having to wait for a popstate 事件。

方法

History interface doesn't inherit any methods.

back()
This asynchronous method goes to the previous page in session history, the same action as when the user clicks the browser's Back button. Equivalent to history.go(-1) .
Calling this method to go back beyond the first page in the session history has no effect and doesn't raise an exception.
forward()
This asynchronous method goes to the next page in session history, the same action as when the user clicks the browser's Forward button; this is equivalent to history.go(1) .
Calling this method to go forward beyond the most recent page in the session history has no effect and doesn't raise an exception.
go()
Asynchronously loads a page from the session history, identified by its relative location to the current page, for example -1 for the previous page or 1 for the next page. If you specify an out-of-bounds value (for instance, specifying -1 when there are no previously-visited pages in the session history), this method silently has no effect. Calling go() without parameters or a value of 0 reloads the current page. Internet Explorer lets you specify a string, instead of an integer, to go to a specific URL in the history list.
pushState()
Pushes the given data onto the session history stack with the specified title (and, if provided, URL). The data is treated as opaque by the DOM; you may specify any JavaScript object that can be serialized.  Note that all browsers but Safari currently ignore the title parameter. For more information, see Working with the History API .
replaceState()
Updates the most recent entry on the history stack to have the specified data, title, and, if provided, URL. The data is treated as opaque by the DOM; you may specify any JavaScript object that can be serialized.  Note that all browsers but Safari currently ignore the title parameter. For more information, see Working with the History API .

规范

规范 状态 Comment
HTML Living Standard
The definition of 'History' in that specification.
实时标准 添加 scrollRestoration 属性。
HTML5
The definition of 'History' in that specification.
Recommendation 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
History Chrome 1 Edge 12 Firefox 1 IE 10 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
back Chrome Yes Edge 12 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
forward Chrome Yes Edge 12 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
go Chrome Yes Edge 12 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
length Chrome Yes Edge 12 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
pushState Chrome 5 Edge 12 Firefox 4
4
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 10 Opera 11.5 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4
4
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 11.5 Safari iOS 4.3 Samsung Internet Android 1.0
replaceState Chrome 5 Edge 12 Firefox 4
4
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 10 Opera 11.5 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4
4
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 11.5 Safari iOS 4.3 Samsung Internet Android 1.0
scrollRestoration Chrome 46 Edge 79 Firefox 46 IE No Opera 33 Safari Yes WebView Android No Chrome Android 46 Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android 5.0
state Chrome Yes Edge 12 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。

另请参阅