检测关闭浏览器、浏览器的返回按钮、关闭微信页面


 1 //检测浏览器关闭
 2     window.onbeforeunload = function () {
 3         console.log("确定离开吗")
 4         return main.getApi();//调用相关需求
 5         
 6     };
 7     $(function(){
 8         pushHistory();
 9         window.addEventListener("popstate", function(e) {
10             // alert("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能
11             main.getApi();//调用相关需求
12         }, false);
13         function pushHistory() {
14             var state = {
15                 title: "title",
16                 url: "#"
17             };
18             window.history.pushState(state, "title", "#");
19         }
20     });
21     //关闭微信页面
22     function weixinClosePage() {
23         if (typeof WeixinJSBridge == "undefined") {
24             if (document.addEventListener) {
25                 document.addEventListener('WeixinJSBridgeReady', weixin_ClosePage, false);
26             } else if (document.attachEvent) {
27                 document.attachEvent('WeixinJSBridgeReady', weixin_ClosePage);
28                 document.attachEvent('onWeixinJSBridgeReady', weixin_ClosePage);
29             }
30         } else {
31             weixin_ClosePage();
32             main.getApi();//调用相关需求
33         }
34     }
35     function weixin_ClosePage() {
36         WeixinJSBridge.call('closeWindow');
37         main.getApi();//调用相关需求
38     }