阻止浏览器回退

$(document).ready(function(e) {
var counter = 0;
if (window.history && window.history.pushState) {
$(window).on('popstate', function () {
alert(1111)
history.back(-1)
window.history.pushState('forward', null, '#');
window.history.forward(1);
alert("不可回退");
});
}

window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
window.history.forward(1);
});

【以下没有进行验证】

input type="button" name="back" value="重新填写" onclick="javascript:history.back(-1);"/>
history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面
history.go(-1):也是返回当前页的上一页,不过表单里的数据全部还在

////////////

javascript做页面后退常使用的方法是

window.history.back();

这样确实可以做到后退的功能,但是项目中,常常并不只是后退就能完成需求,往往需要在后退的同时,刷新后退的页面信息,比如后退到首页同时刷新首页的最新数据,这样的需求通过上面这种方法就没法满足了,为了实现这个需求,我们需要使用到

document.referrer

这个方法可以取到上一个页面的具体路径,我们通过这个方法,再结合JS的跳转函数

window.location.href

就可以实现后退并且刷新的效果的,完整代码如下:

window.location.href=document.referrer;

作者:有梦想的乌龟·
原文地址:https://www.cnblogs.com/nlbnick/p/6979550.html