背景:在很多系统,一些界面都是用户手动点击退出按钮的。但是,为了避免,用户误操作点击游览器后退,或者用鼠标手势后退什么的,出现误操作。所以,适当的禁用游览器的后退,是可以很大的提高用户的体验的。
一、js代码
这种方法可以消除所有的后退动作,包括:键盘/鼠标手势等产生的后退动作。
<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);
}
参考博客:利用js实现 禁用游览器后退 https://www.cnblogs.com/sunshq/p/7976827.html