快乐每一天

自己选择的路,跪着也要走下去...

标签 js物理返回 下的文章

Javascript控制页面物理返回事件处理

点击物理返回键.关闭窗口

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

点击物理返回键,返回到指定页面

window.addEventListener("popstate", function(e) {
    window.location.href="{:url('sdns/index/index')}";
}, false);
function pushHistory() { 
    var state = { 
        title: "title", 
        url: "#"
    }; 
    window.history.pushState(null,null, "#"); 
}

禁用物理返回键

XBack = {};
(function(XBack) {
    XBack.STATE = 'x - back';
    XBack.element;
    XBack.onPopState = function(event) {
        event.state === XBack.STATE && XBack.fire();
        XBack.record(XBack.STATE); //初始化事件时,push一下
    };
    XBack.record = function(state) {
        history.pushState(state, null, location.href);
    };
    XBack.fire = function() {
        var event = document.createEvent('Events');
        event.initEvent(XBack.STATE, false, false);
        XBack.element.dispatchEvent(event);
    };
    XBack.listen = function(listener) {
        XBack.element.addEventListener(XBack.STATE, listener, false);
    };
    XBack.init = function() {
        XBack.element = document.createElement('span');
        window.addEventListener('popstate', XBack.onPopState);
        XBack.record(XBack.STATE);
    };
})(XBack); // 引入这段js文件
XBack.init();
XBack.listen(function() {});

禁止返回并给个提示

pushHistory(); 
window.addEventListener("popstate", function(e) { 
    popTip("禁止返回");
    XBack = {};
    (function(XBack) {
        XBack.STATE = 'x - back';
        XBack.element;
        XBack.onPopState = function(event) {
            event.state === XBack.STATE && XBack.fire();
            XBack.record(XBack.STATE); //初始化事件时,push一下
        };
        XBack.record = function(state) {
            history.pushState(state, null, location.href);
        };
        XBack.fire = function() {
            var event = document.createEvent('Events');
            event.initEvent(XBack.STATE, false, false);
            XBack.element.dispatchEvent(event);
        };
        XBack.listen = function(listener) {
            XBack.element.addEventListener(XBack.STATE, listener, false);
        };
        XBack.init = function() {
            XBack.element = document.createElement('span');
            window.addEventListener('popstate', XBack.onPopState);
            XBack.record(XBack.STATE);
        };
    })(XBack); // 引入这段js文件
    XBack.init();
    XBack.listen(function() {});
}, false);
function pushHistory() { 
    var state = { 
        title: "title", 
        url: "#"
    }; 
    window.history.pushState(null,null, "#"); 
}
阅读全文