微信公众号监听页面返回事件

        function pushHistory(){
          var state = {
            title: "",
            url: "#"
          };
          window.history.pushState(state, state.title, state.url);
        }
  
        function win_close(){
          if (typeof WeixinJSBridge != "undefined") {
            WeixinJSBridge.call("closeWindow");
          } else {
            if (navigator.userAgent.indexOf("MSIE") > 0) {
              if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                window.opener = null;
                window.close();
              } else {
                window.open("", "_top");
                window.top.close();
              }
            } else if (navigator.userAgent.indexOf("Firefox") > 0) {
              window.location.href = "about:blank ";
            } else {
              window.opener = null;
              window.open("", "_self", "");
              window.close();
            }
          }
        }

        (function(){
          pushHistory();
          window.addEventListener("popstate",function(e) {
              //alert("触发了页面返回事件")   可以放实际业务代码
              win_close();    
            },false);
        })();

 

你可能感兴趣的:(微信公众号监听页面返回事件)