history对象
包含用户(在浏览器窗口中)访问过的 URL。
History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。
注意:没有应用于 History 对象的公开标准,不过所有浏览器都支持该对象。
History 对象属性
1. length
声明了浏览器历史列表中的元素数量
注意:Internet Explorer和Opera从0开始,而Firefox、Chrome和Safari从1开始。
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
history.length
document.write("历史列表中URL的数量: " + history.length);
History 对象方法
1. back
可加载历史列表中的前一个 URL(如果存在)
调用该方法的效果等价于点击后退按钮或调用 history.go(-1)。
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
history.back()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function goBack(){
window.history.back() document.write(document.body.innerHTML +="历史列表中URL的数量: " + history.length);
}
</script>
</head>
<body>
<input type="button" value="返回" onclick="goBack()">
<a href="#" target="_self">点我点我</a>
</body>
</html>
2. forward
可加载历史列表中的下一个 URL(如果存在)
调用该方法的效果等价于点击前进按钮或调用 history.go(1)。
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
history.forward()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function goBack(){
window.history.back()
document.write(document.body.innerHTML +="历史列表中URL的数量: " + history.length);
goBack = function(){
window.history.forward();
document.write("现在前进了");
}
}
</script>
</head>
<body>
<input type="button" value="返回" onclick="goBack()">
<a href="#" target="_self">点我点我</a>
</body>
</html>
3. go
可加载历史列表中的某个具体的页面
该参数可以是数字,使用的是要访问的 URL 在 History 的 URL 列表中的相对位置。(-1上一个页面,1前进一个页面)。或一个字符串,字符串必须是局部或完整的URL,该函数会去匹配字符串的第一个URL。
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
history.go(number|URL)
参数
- 必需
-
- number 使用的是要访问的 URL 在 History 的 URL 列表中的相对位置。(-1上一个页面,1前进一个页面) 或者 URL 字符串必须是局部或完整的URL,该函数会去匹配字符串的第一个URL。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function goBack(){
window.history.go(-2)
}
</script>
</head>
<body>
<input type="button" value="后退2页" onclick="goBack()">
</body>
</html>
</script>
</head>
<body>
<input type="button" value="返回" onclick="goBack()">
<a href="#" target="_self">点我点我</a>
</body>
</html>
文档内容出自 W3cSchool和菜鸟教程,
如需查看更详细的有关内容 请登录 http://www.w3school.com.cn/ 和 http://www.runoob.com/