当前位置: 首页 > Web前端 > HTML5

监听浏览器的返回事件

时间:2023-04-05 00:33:47 HTML5

1。窗口的popstate事件监听浏览器的后退事件,主要靠监听:窗口的popstate事件,因为浏览器在点击“后退”或“前进”按钮时会触发popstate事件。因此,可以在window.on('popstate',function(){//todo})2.window的pushState事件中添加浏览器的历史记录,但是不刷新当前页面的函数,即不请求已发生导致window.location.href更改。这个方法有三个参数,分别是:state:要添加的记录的状态,是一个对象title:新页面的标题,可以为空href:新页面的urlAdd$(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将当前页面推送到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的,这里只是代码的搬运工,记录一下,以后修改。