js监听返回事件,返回直接关闭窗口事件(微信网页返回关闭)

$(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", "");
    }
});

 

posted @ 2017-12-22 11:12  jhycwnqry  阅读(9039)  评论(0编辑  收藏  举报