微信H5网页 禁止浏览器回退/返回

//禁止浏览器回退/返回
XBack = {};
(function (XBack) {
    XBack.STATE = 'x - back';
    XBack.element;
    XBack.onPopState = function (event) {
        event.state === XBack.STATE && XBack.fire();
        XBack.record(XBack.STATE);
    };
    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);
XBack.init();
XBack.listen(function () { });

把这段代码放到 需要禁止返回的那一页上就行了!

你可能感兴趣的:(微信H5网页,禁止返回,前端,JS,微信H5网页)