h5 browser js monitor returns

h5 js monitors the return event and returns the event of directly closing the window

$(function(){
    pushHistory();
    window.addEventListener("popstate", function(e) {
        //alert("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能
        //alert('guanbi');
        WeixinJSBridge.call('closeWindow');
        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 ';  
                    //window.history.go(-2);  
                }  
                else {  
                    window.opener = null;   
                    window.open('', '_self', '');  
                    window.close();  
                }
            }
    }, false);
    window.onbeforeunload = function() {
            return;
        } 
    function pushHistory() {
        var state = {
            title: "title",
            url: ""
        };
        window.history.pushState(state, "title", "");
    }
});

 js monitors the browser leaving the page operation

Overview

  Both onunload and onbeforeunload are called when refreshing or closing, and can be specified in the <script> script through window.onunload or in <body>. The difference is that onbeforeunload is executed before onunload, it can also prevent the execution of onunload. onbeforeunload is also called when the page is refreshed or closed. onbeforeunload is called when the server is about to read a new page, and it has not started reading at this time; and onunload has read the new page that needs to be loaded from the server and will be replaced soon Called when the current page is dropped. Onunload cannot prevent the page from being updated and closed, and onbeforeunload can do it.

  Attached:

    Only execute onload when the page loads. Onbeforeunload is executed 
    first when the page is closed, and finally
    onbeforeunload, then onunload, and finally onload when the onunload  page refreshes.  

 

<!DOCTYPE html>
    <head>
        <meta charset="UTF-8">
        <title>测试</title>
        <script>
          function checkLeave(){
            event.returnValue="确定离开当前页面吗?";
          }
      </script>
    </head>
    <body onbeforeunload="checkLeave()">
        测试
    </body>
</html>

 

 

 

Published 173 original articles · 29 praises · 290,000 views

Guess you like

Origin blog.csdn.net/qq_29058883/article/details/105430428