利用js实现 禁用浏览器后退
编辑时间:2022-01-21 作者:网络 浏览量:1875 来源:网络

 也是查找了好多资料才找到的,这种方式,可以消除 后退的所有动作。包括 键盘、鼠标手势等产生的后退动作。

 <script language="javascript">
        //防止页面后退
        history.pushState(null, null, document.URL);
        window.addEventListener('popstate', function () {
            history.pushState(null, null, document.URL);
        });
    </script>


现在,我们项目中就使用了第三种方式。在常用浏览器中,都可以禁用了后退。

//禁用浏览器返回 function fobidden_back() {
    //防止页面后退
    history.pushState(null, null, document.URL);
    window.addEventListener('popstate',back_common)
}
//启用浏览器返回 function enable_back() {
    history.go(-1);
    window.removeEventListener('popstate',back_common)
} function back_common() {
    history.pushState(null, null, document.URL);
}


mb里可以直接配置项禁用回退

wkeSetDebugConfig(webview, "backKeydownEnable", "0")


来说两句吧