有关页面跳转

本文详细介绍了多种页面跳转方法,包括a标签、window.location.href、window.location.replace、window.history.forward及back等,并对比了它们对history.length的影响及数据缓存特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现跳转的方式大概有如下几种:

<body>
    <div>...b页面...</div>
    <input type="text" placeholder="请输入...">
    <div>
        <a href="/a.html">[a标签]跳转到a页</a>
    </div>
    <div id="_b1">[window.location.href]跳转到a页</div>
    <div id="_b2">[window.location.replace()]跳转到a页</div>
    <div id="_b3">[window.history.forward()]前进一页</div>
    <div id="_b4">[window.history.back()]后退一页</div>
    <script>
        alert('b页面重新执行啦')
        document.getElementById('_b1').onclick=function(){
            window.location.href = '/a.html';
        }
        document.getElementById('_b2').onclick=function(){
            window.location.replace('/a.html');
        }
        document.getElementById('_b3').onclick=function(){
            window.history.forward();
            // 或者写成
            // window.history.go(1);
        }
        document.getElementById('_b4').onclick=function(){
            window.history.back();
            // 或者写成
            // window.history.go(-1);
        } 
    </script>
</body>

除此之外,还包括浏览器自带的 "前进" 和 "后退" 按钮;

以上所有的页面跳转方式都会导致页面js的重新执行;

"前进" 按钮 效果等同于 window.history.forward() 或者 window.history.go(1);

      >>>>>> 这种方式只是 前进到下一个页面,不会更改history.length,前进到的页面也会缓存之前的数据;

"后退" 按钮 效果等同于 window.history.back() 或者 window.history.go(-1);

     >>>>>> 这种方式只是 后退到下一个页面,不会更改history.length,后退到的页面也会缓存之前的数据;

例如:在b页面输入一些内容,然后再跳转到a页面,然后再从a页面点击"后退" 按钮 或者 执行window.history.back() 或者 执行window.history.go(-1) 这三种方式回到b页面,那么b页面之前输入的内容依然会存在。

a标签 和 window.location.href 实现的页面跳转则会增加 window.history.length;

window.location.replace() 该方法实现的页面跳转则是替换当前的页面地址,同样也不会改变 window.history.length;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值