在 iOS 上的 Safari 中调用 history.pushState
后,将无法再使用 alert()
、confirm()
或 prompt()
,当使用浏览器后退按钮变回时。
这是 iOS 错误吗?是否有任何已知的解决方法?
重现此行为的简单示例:
<html>
<body>
<ul>
<li>Step 1: <button onclick="alert(Math.random())">Confirm Alert is working</button></li>
<li>Step 2: <button onclick="history.pushState(null, null, '/debug/'+Math.random());">Change History</button></li>
<li>Step 3: use your browser back button, to go back</li>
<li>Step 4: <button onclick="alert(Math.random())">Alert is not working anymore</button></li>
</ul>
</body>
</html>
您可以在这里在线试用:goo.gl/faFW6o .
最佳答案
这是因为 Safari 中的前后缓存。
您可以使用以下代码在按下后退按钮时强制重新加载。
window.onpageshow = function(e) { // e -> event
if (e.persisted) {
window.location.reload();
}
};
此外,如果您使用的是 jQuery ...
$(window).bind("pageshow", function(e) { // e -> event
if (e.originalEvent.persisted) {
window.location.reload();
}
});
关于javascript - 在 Safari、iOS 上使用 History API 后警告、确认和提示不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38083702/