History对象使用小结

发表于: sitebuild | 作者: darkmi | 日期: 2015/07/14 08:00
标签: History
字号:

简介

History 对象包含用户(在浏览器窗口中)访问过的 URL。History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。History 对象最初设计来表示窗口的浏览历史。但出于隐私方面的原因,History 对象不再允许脚本访问已经访问过的实际 URL。唯一保持使用的功能只有 back()、forward() 和 go() 方法。

注意:

没有应用于 History 对象的公开标准,不过所有浏览器都支持该对象。

History 对象属性

length

返回浏览器历史列表中的 URL 数量。

History 对象方法

back()

加载 history 列表中的前一个 URL。

forward()

加载 history 列表中的下一个 URL。

go()

加载 history 列表中的某个具体页面。

例子

下面一行代码执行的操作与单击后退按钮执行的操作一样:

history.back()

下面一行代码执行的操作与单击两次后退按钮执行的操作一样:

history.go(-2)

其他示例:

<input type=button value="刷新" onclick="window.location.reload()">
<input type=button value="前进" onclick="window.history.go(1)">
<input type=button value="后退" onclick="window.history.go(-1)">
<input type=button value="前进" onclick="window.history.forward()">
<input type=button value="后退" onclick="window.history.back()">
<input type=button value="后退+刷新" onclick="window.history.go(-1);window.location.reload()">

以下两点需要注意一下:

window.history.go(-1)

返回上一页

window.location.go(-1)

刷新上一页

顺道总结一下Javascript刷新页面的几种方法:

history.go(0)
location.reload()
location=location
location.assign(location)
document.execCommand('Refresh')
window.navigate(location)
location.replace(location)
document.URL=location.href

来源:http://www.w3school.com.cn/jsref/dom_obj_history.asp

切换主题切换主题
返回顶部