阻止瀏覽器回退
$(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;
阻止瀏覽器回退