方案一:
<script language="javascript">
//防止页面后退
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
方案二:
//封装的常规操作,停留在本页面
function stay() {
console.log('兄台留步');
history.forward();
}
if (!(history.state && history.state.target == "Final")) {
window.history.pushState({target: "MeanSure", random: Math.random()}, "", location.href);
window.history.pushState({target: "Final", random: Math.random()}, "", location.href);
}
window.addEventListener("popstate", function (e) {
if (e.state && e.state.target == "MeanSure") {
//此处可调用一些自定义的操作,例如弹窗提示之类的
stay(); // 如此操作会停留在本页面
}
}, false);
网友评论