0

如何防止popstate页面加载时触发事件?(这发生在 Android 浏览器上)

在 Android 浏览器上运行以下代码段并查看结果。(替代链接 ro 运行代码段:https ://jsfiddle.net/heoceze0/1 )

window.onpopstate = function (e) {
  alert('popstate is called!!!');
};
If you got no alert, that's ok...

4

1 回答 1

-2

popstate每次活动历史条目在同一文档的两个历史条目之间发生变化时,都会向窗口发送一个事件。

为了防止它触发,只需使用event.preventDefault(),

window.onpopstate = function (event) {
  event.preventDefault();
  alert('popstate is called!!!');
};
于 2016-07-13T09:40:43.007 回答