知识学堂 > 课程 > 微信禁止返回上一页的一种方案

微信禁止返回上一页的一种方案

发布日期:2019/11/2 来源:聚恒【返回】

history.pushState(null, null, document.URL);
window.addEventListener("popstate",function(e) {  
  console.log(e);
  history.pushState(null, null, document.URL);
}, false);


监听浏览器返回按钮1

<script>
    $(function () {
        pushHistory();
        var bool = false;
        setTimeout(function () {
            bool = true;
        }, 1500);
        window.addEventListener("popstate", function (e) {
            if (bool) {
                //                alert("我监听到了浏览器的返回按钮事件啦");
                WeixinJSBridge.call('closeWindow');
            }
        }, false);
    });  
    function pushHistory() {
        var state = {
            title: "title",
            url: "__SELF__"
        };
        window.history.pushState(state, state.title, state.url);
    }
</script>

监听浏览器返回按钮2

function pushHistory() {  
  var state = {  
    title: "title",  
    url: "#"  
  };  
  window.history.pushState(state, "title", "#xx");  
}
pushHistory();
window.addEventListener("popstate", function(e) {  
  console.log(e);
  alert("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能  
}, false);