History
接口允许操作浏览器的曾经在标签页或者框架里访问的会话历史记录。
属性
History
接口不继承于任何属性。
History.length
只读- 返回一个整数,该整数表示会话历史中元素的数目,包括当前加载的页。例如,在一个新的选项卡加载的一个页面中,这个属性返回1。
History.current
只读 已废弃 Gecko 26- 返回一个代表session历史记录中活动的项目URL的
DOMString
,这个属性永远对web内容不可用并且已经不再被任何浏览器支持。可以使用Location.href
来代替它。 History.next
只读 已废弃 Gecko 26- Returns a
DOMString
representing the URL of the next item in the session history. This property was never available to web content and is not supported by other browsers. History.previous
只读 已废弃 Gecko 26- Returns a
DOMString
representing the URL of the previous item in the session history. This property was never available to web content and is not supported by other browsers. History.scrollRestoration
- 允许Web应用程序在历史导航上显式地设置默认滚动恢复行为。此属性可以是自动的(auto)或者手动的(manual)。
History.state
只读- 返回一个表示历史堆栈顶部的状态的值。这是一种可以不必等待
popstate
事件而查看状态而的方式。
方法
History接口不继承任何方法。
History.back()
- 前往上一页, 用户可点击浏览器左上角的返回按钮模拟此方法. 等价于
history.go(-1)
.Note: 当浏览器会话历史记录处于第一页时调用此方法没有效果,而且也不会报错。 History.forward()
- 在浏览器历史记录里前往下一页,用户可点击浏览器左上角的前进按钮模拟此方法. 等价于
history.go(1)
.Note: 当浏览器历史栈处于最顶端时( 当前页面处于最后一页时 )调用此方法没有效果也不报错。 History.go()
- 通过当前页面的相对位置从浏览器历史记录( 会话记录 )加载页面。比如:参数为-1的时候为上一页,参数为1的时候为下一页. 当整数参数超出界限时( 译者注:原文为When
integerDelta
is out of bounds ),例如: 如果当前页为第一页,前面已经没有页面了,我传参的值为-1,那么这个方法没有任何效果也不会报错。调用没有参数的go()
方法或者不是整数的参数时也没有效果。( 这点与支持字符串作为url参数的IE有点不同)。 History.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 Firefox currently ignores the title parameter; for more information, see manipulating the browser history.
Note: In Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) through Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the passed object is serialized using JSON. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
History.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 Firefox currently ignores the title parameter; for more information, see manipulating the browser history.
Note: In Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) through Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the passed object is serialized using JSON. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard History |
Living Standard | Adds the scrollRestoration attribute. |
HTML5 History |
Recommendation | Initial definition. |
Custom Scroll Restoration - History-based API History |
Editor's Draft | Adds the scrollRestoration attribute. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
pushState and replaceState |
5.0 | 4.0 | 10 | 11.5 | 5 |
scrollRestoration |
46.0 | 46.0 (46.0) | 未实现 | 33 | 未实现 |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
pushState and replaceState |
2.2 | (Yes) | (Yes) | 10 | (Yes) | 4.3 | (Yes) |
scrollRestoration |
未实现 | 46.0 | (Yes) | 未实现 | (Yes) | 未实现 | 46.0 |
See also
- The
Window.history
property returning the history of the current session.