如何清除windows`push`状态

时间:2018-02-21 06:24:41

标签: javascript jquery

我正在使用history.push()州来阻止用户根据客户要求提供网页。

为此,我使用以下代码:

history.pushState(null, null, document.URL);
    $(window).on('popstate', function () {
        history.pushState(null, null, document.URL);
});

工作正常。但是在下一页中,我无法使用后退按钮,因为上面的代码设置在全局空间中。 那么如何清除历史推送状态任何使浏览器再次正常?

提前致谢。

1 个答案:

答案 0 :(得分:0)

理想情况下pushState用于将新网址推送到地址栏。

您需要为popstate事件添加一个侦听器。

当您按下状态后单击后退按钮时,页面将接收事件。

在其中,您需要使用正确的页面替换页面内容。

window.onpopstate = function(event) {    
    if ( event && event.state ) {
        location.reload(); 
    }
}