【问题标题】:window popstate event not working in safari窗口弹出状态事件在 Safari 中不起作用
【发布时间】:2016-11-02 13:55:02
【问题描述】:

我已尝试通过此代码禁用浏览器后退按钮:

<script>
    //disable back button
    history.pushState(null, null, ' '); 
    window.addEventListener('popstate', function () {
        history.pushState(null, null, ' ');
    });
</script>

它适用于 ChromeMozilaIE10,但不适用于 safari(5.x)。
在跨浏览器之前有没有人做到这一点?

【问题讨论】:

  • 我进一步查看了 Safari 支持 pushSate ,但 popstate 事件没有触发
  • 它适用于最新的 Safari。
  • 请用我的代码试试,让我知道状态。
  • 解决了你的问题吗?

标签: javascript safari browser-history


【解决方案1】:

按照下面的代码,

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

此代码在 safari 5.1.7 上运行良好。 您可以使用此代码进行测试并让我知道状态。

【讨论】:

  • 那么您应该接受答案,并为 stackoverflow 的其他用户投票。 @AnimeshKumarSharma
【解决方案2】:
history.pushState(null, null, Url);
window.addEventListener('popstate', function(event) {
    history.pushState(null, null, Url);
});

这很好,但我也有另一种 safari 浏览器的解决方案:

history.replaceState(null, null, 'url');
history.pushState(null, null, 'url');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多