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(-)
}
</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/