博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
监听浏览器的后退事件
阅读量:7005 次
发布时间:2019-06-27

本文共 1143 字,大约阅读时间需要 3 分钟。

1.window的popstate事件

监听浏览器回退事件,主要依赖于监听:window的popstate事件,因为浏览器在被点击“后退”或者“前进"按钮时,都会触发popstate事件。

因此,可以在

window.on('popstate', function(){    // to do})

2.window的pushState事件

window的pushState事件,是实现往浏览器的历史记录中添加记录,但是不刷新当前页面的功能,即没有发生任何请求导致window.location.href发生变化。此方法有三个参数,分别是:

state:要新添加的记录的状态,是个对象title:新页面的title,可以为空href:新页面的url

3. 监听浏览器后退的实现

3.1 将当前页面的URL后面加
$(document).ready(function (e) {        if (window.history && window.history.pushState) {            $(window).on('popstate', function () {                self.location="/credit/miniprogram/copartner/bankList"; //如查需要跳转页面就用它            });        }        window.history.pushState('forward', null, '#'); //在IE中必须得有这两行        window.history.forward(1);    });
3.2 将当前页面压入历史记录中
"pushState" in window.history && (    window.history.pushState({        title: document.title,        url: location.href    }, document.title, location.href),      //setTimeout(function () {          window.addEventListener("popstate", function (a) {            self.location="/credit/miniprogram/copartner/bankList";                              })      //})    );

PS: 不是特别明白pushState的作用,这里只是代码的搬运工,记录一下,后续修改吧。

转载地址:http://esytl.baihongyu.com/

你可能感兴趣的文章
C# WPF vs WinForm
查看>>
Java内部类总结
查看>>
Cognos 图表用图片取代”没有数据显示”
查看>>
51 EEPROM操作模板
查看>>
Fedora 15 KDE中如何打开software management及如何应用
查看>>
Hadoop之回收站
查看>>
ESRI ArcGIS 9.0系列软件报价(转)
查看>>
Google Plugin for Eclipse
查看>>
FIREDAC驱动ORACLE的配置
查看>>
IOS数据类型对应输出格式
查看>>
GDI+中发生一般性错误的解决办法(转)
查看>>
PIL(Python Image Library)生成验证码
查看>>
中文分词器ICTCLAS使用方法(Java)
查看>>
UX最佳实践:提高用户体验影响力的艺术
查看>>
移动互联:用户体验设计指南
查看>>
BZOJ 2666: [cqoi2012]组装
查看>>
iOS 开发笔记-Objective-C之KVC、KVO
查看>>
PHP5与MySQL数据库操作
查看>>
在界面中显示文本内容
查看>>
DVI-D是虾米东东
查看>>