Live2d Test Env

vue禁用浏览器返回功能

 1 // 引入jq
 2 mounted() { // 看好周期
 3     disableBrowserBack();
 4     history.pushState(null, null, document.URL);
 5     if (window.history && window.history.pushState) {
 6         $(window).on('popstate', function () 
 7             window.history.pushState('forward', null, '');
 8             window.history.forward(1);
 9         });
10     }
11     //
12     window.history.pushState('forward', null, '');  //在IE中必须得有这两行
13     window.history.forward(1);
14 },

 

或者提到公共js中

util.js

 1 import "@/xb-share/api/jquery-1.11.3.min.js";
 2  
 3 export const disableBrowserBack = (param) => {
 4     return (function () {
 5         history.pushState(null, null, document.URL);
 6         if (window.history && window.history.pushState) {
 7             $(window).on('popstate', function () {
 8                 window.history.pushState('forward', null, '');
 9                 window.history.forward(1);
10             });
11         }
12         //
13         window.history.pushState('forward', null, '');
14         window.history.forward(1);
15     })()
16 };

页面中引用

1 import {disableBrowserBack} from "../../xb-share/util/util";
2 
3 
4 mounted() {
5     disableBrowserBack()
6 },

就可以了

posted @ 2019-12-04 17:06  小猪(朱)  阅读(1459)  评论(0编辑  收藏  举报