问题描述
在iOS上的Safari中调用history.pushState
后,使用浏览器的后退按钮改回时,将无法再使用alert()
,confirm()
或prompt()
.
After calling history.pushState
in Safari on iOS, it's no longer possible to use alert()
, confirm()
or prompt()
, when using the browser back button to change back.
这是iOS错误吗?有任何已知的解决方法吗?
Is this an iOS bug? Are there any known workarounds?
重现此行为的简单示例:
Simple example to reproduce this behavior:
<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 .
You can try it online here: goo.gl/faFW6o.
推荐答案
这是因为Safari中的后退缓存.
This is because of the back-forward cache in Safari.
您可以使用以下代码在按下后退按钮时强制重新加载.
You can use the following code to force a reload when the back-button is pressed.
window.onpageshow = function(e) { // e -> event
if (e.persisted) {
window.location.reload();
}
};
此外,如果您使用的是jQuery ...
Additionally, if you are using jQuery ...
$(window).bind("pageshow", function(e) { // e -> event
if (e.originalEvent.persisted) {
window.location.reload();
}
});
这篇关于在Safari,iOS上使用History API后发出警报,确认并提示不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!