Window-history对象

6 篇文章 0 订阅
4 篇文章 0 订阅

history对象

包含用户(在浏览器窗口中)访问过的 URL。

History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。

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

History 对象属性

1. length

声明了浏览器历史列表中的元素数量

注意:Internet Explorer和Opera从0开始,而Firefox、Chrome和Safari从1开始。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue

history.length

document.write("历史列表中URL的数量: " + history.length);

History 对象方法

1. back

可加载历史列表中的前一个 URL(如果存在)

调用该方法的效果等价于点击后退按钮或调用 history.go(-1)。

浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue

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)。

浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue

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。

浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值