js监控微信浏览器的自带的返回事件(延迟解决微信返回立即执行popstate事件)
/** * 浏览器回退事件监听 */ var listenerBackHandler = { param: { isRun: false, //防止微信返回立即执行popstate事件 }, listenerBack: function () { var state = { title: "title", url: "#" }; window.history.pushState(state, "title", "#"); window.addEventListener("popstate", function (e) { if (listenerBackHandler.param.isRun) { window.location.href = "Home/Index"; //返回到主页 } } }, false); }, //初始化返回事件 initBack: function () { window.addEventListener('pageshow', function () { listenerBackHandler.param.isRun = false; setTimeout(function () { listenerBackHandler.param.isRun = true; }, 1000); //延迟1秒 防止微信返回立即执行popstate事件 listenerBackHandler.listenerBack(); }); } }
A journey of a thousand miles begins with a single step!