go()
方法从会话历史记录中加载特定页面。你可以使用它在历史记录中前后移动,具体取决于delta
参数的值。
语法
window.history.go(delta);
参数
delta
可选- 相对于当前页面你要去往历史页面的位置。负值表示向后移动,正值表示向前移动。因此,例如:
history.go(2)
向前移动两页,history.go(-2)
则向后移动两页。如果未向该函数传参或delta
相等于0,则该函数与调用location.reload()
具有相同的效果。 -
译者注:
相等于0是采用宽松相等进行比较的。另外,JavaScript值古怪的隐式转换在这里也是可用的。
示例
向后移动一页(等价于调用back()
):
window.history.go(-1)
向前移动一页,就像调用了forward()
:
window.history.go(1)
向前移动两页:
window.history.go(2);
向后移动两页:
window.history.go(-2);
最后,以下任意一条语句都会重新加载当前页面:
window.history.go();
window.history.go(0);
规范
Specification | Status | Comment |
---|---|---|
HTML Living Standard History |
Living Standard | No change from HTML5. |
HTML5 History |
Recommendation | Initial definition. |
浏览器兼容性
BCD tables only load in the browser
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.