Deprecated: Function create_function() is deprecated in /www/wwwroot/blog.fenewbee.com/usr/plugins/AntiSpam/Plugin.php on line 75
H5中 JS 禁用安卓手机物理返回键 , 微信浏览器中也支持 - A Maverick Pig - - Life is more than just the present, but also poetry and the distance.

A Maverick Pig -

生活不止眼前的苟且
还有诗和远方

H5中 JS 禁用安卓手机物理返回键 , 微信浏览器中也支持

直接引入以下代码就可以实现

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() {});  
本原创文章未经允许不得转载 | 当前页面:A Maverick Pig - » H5中 JS 禁用安卓手机物理返回键 , 微信浏览器中也支持

评论