js 捕获浏览器后退事件

$(document).ready(function(e) {
            var counter = 0;
            if (window.history && window.history.pushState) {
            window.onpopstate = function () {
                        window.history.pushState(‘forward‘, null, ‘#‘);
                        window.history.forward(1);
                        alert("不可回退");
                    };
            }
            window.history.pushState(‘forward‘, null, ‘#‘); //在IE中必须得有这两行
            window.history.forward(1);
});

原文地址:https://www.cnblogs.com/sea-stream/p/9029555.html

时间: 2024-08-05 04:37:11

js 捕获浏览器后退事件的相关文章

模拟浏览器后退事件

在工作中碰到一个需求:点击浏览器的后退按钮时,需要判断当前页面的表单数据是否已保存:如果没保存,就给出提示.但浏览器并没有后退事件,这个项目也没有用什么 MV** 之类的框架或库,用的是 jQuery,于是只能尝试模拟浏览器后退事件. 用的是 H5 中的 pushState 与 popstate,代码如下: var onBrowserBack = function(callback) { var userAgent = navigator.userAgent.toLowerCase(), isS

js 判断浏览器关闭事件 兼容所有浏览器

无论是从页签处关闭浏览器,还是关闭整个浏览器窗口,无论是 ie11,火狐,谷歌,苹果,还是ie6,都能兼容的浏览器关闭事件监听 在网上搜索了一天,虽然网上也有之类的代码,但是太繁琐,有时候还不可用.我也是在原有基础上修改的.经过了上述的浏览器测试,如果有不兼容的,欢迎提出意见一起学习. ? <script type="text/jscript" src="jquery-1.10.2.min.js"></script> <script t

监听浏览器后退事件,使其转向指定URL

$(function(){ //清空浏览器历史记录 pushHistory(); //监听浏览器后退事件 window.addEventListener("popstate", function(e) { //转向指定的URL location.href=url; }, false); //清空浏览器历史记录 function pushHistory() { var url = "#"; var state = { title: "title",

js实现浏览器后退页面刷新

最近在开发中遇到一个问题: 在一个列表页面,点击进入详情,详情页面对其状态操作,其详情页面有做修改,然后点击浏览器后退,返回到列表页,在列表页面状态还是操作之前的,为解决状态统一需要手动刷新改列表页.通过一番折腾,终于查到相关的解决方案,实现方式如下: 1.在页面中增加隐藏域  <input type="hidden" id="refreshed" value="no"> 2.通过js设置和获取隐藏域值来自动刷新页面 $(functi

js 跨浏览器实现事件

我们知道不同的浏览器实现事件是不同的,就比如说我们常见的有三种方法: 1,dom0处理事件的方法,以前的js处理事件都是这样写的. (function () { var p=document.getElementById("huchao1"); //dom0处理方法 p.onclick=function (a) { console.log(a.type); // body... } //移除方法如下: p.onclick=null; // body... })() 2,dom2 处理时

js 监控浏览器关闭事件

代码如下: <!DOCTYPE html> <html> <head> <title>监控浏览器关闭事件</title> </head> <style type="text/css"> </style> <body> <div id="create_order"> </div> </body> </html> &

js 监听后退事件及跳转页面

//直接跳转 window.location.href="b.html"; //返回上一级页面 window.history.back(-1); //返回下一级页面 window.history.go(-1); jq监听后退事件 <script type="text/javascript"> $(document).ready(function(e) { var counter = 0; if (window.history && win

js监听浏览器后退事件

$(document).ready(function(e) {             var counter = 0;            if (window.history && window.history.pushState) {                             $(window).on('popstate', function () {                                            window.history.

JS禁止浏览器后退键

当键盘敲下后退键(Backspace)后 1.禁止浏览器自动后退 2.但不影响密码.单行文本.多行文本输入框等的回退操作 //方法一: ////当键盘敲下后退键(Backspace)后 //1.禁止浏览器自动后退 //2.但不影响密码.单行文本.多行文本输入框等的回退操作 //window.onload = function () { // /**************************** // * 作者:[email protected] * // * 时间:2012-08-20 *