gpt4 book ai didi

javascript - 为什么 popstate 在 Firefox 中不触发

转载 作者:行者123 更新时间:2023-11-28 09:48:52 25 4
gpt4 key购买 nike

我有这个 html 页面。在 chrome 上,如果我在 Button2 中单击几次,然后单击导航器后退按钮

<html>
<head>
<script>

function button_onclick(){
var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");
}

function onPopState(){
console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
}


window.addEventListener('popstate', onPopState, false);

</script>
</head>

<body>

<button type="button" onclick="button_onclick()">Button 2</button>

</body>

</html>

我可以在控制台中看到:

location: http://somedomain:8080/examples/bar.html, state: {"foo":"bar"} 

如果我尝试使用 Firefox,我什么也看不到?我用Firefow 13.0.1测试如果我想使用 html5 历史记录 API 管理历史记录,那会很烦人

最佳答案

发现另一个问题并帮助我:

popstate isn't supposed to be called when you click the link. It's called when the user navigates with the back/forward button.

Everything is working the way it is supposed to.

关于javascript - 为什么 popstate 在 Firefox 中不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com