【问题标题】:Why does calling History.pushState() (or .replaceState()) trigger a 'popstate' event?为什么调用 History.pushState()(或 .replaceState())会触发“popstate”事件?
【发布时间】:2017-05-18 12:05:18
【问题描述】:

我正在根据 AJAX 调用更改网址。

window.history.pushState("", "", ''+newUrlWithParameters);

我的 JS 中还有一个 popstate 事件。

$(window).on('popstate', function(e) {
   //... do this
});

只要调用 pushState,就会触发 popstate。 如何防止这种情况?

【问题讨论】:

标签: javascript jquery history pushstate popstate


【解决方案1】:

我有同样的问题 - 通过添加解决了它

event.preventDefault();
event.stopPropagation();

到调用事件的处理程序(他们处理 HTML a 元素上的单击事件,并通过 jquery 跟踪 mouseevent 似乎想要触发 popState)。

【讨论】:

    猜你喜欢
    • 2012-06-12
    • 2021-07-16
    • 2019-01-14
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多