[英]Alert, confirm, and prompt not working after using History API on Safari, iOS
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 上的 Safari 中调用history.pushState
后,当使用浏览器后退按钮更改回来时,不再可能使用alert()
、 confirm()
或prompt()
。
Is this an iOS bug?这是iOS错误吗? 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>
You can try it online here: goo.gl/faFW6o .您可以在此处在线试用: goo.gl/faFW6o 。
This is because of the back-forward cache in Safari.这是因为 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();
}
};
Additionally, if you are using jQuery ...此外,如果您使用的是 jQuery ...
$(window).bind("pageshow", function(e) { // e -> event
if (e.originalEvent.persisted) {
window.location.reload();
}
});
It might have been an issue when this question was asked. 当问到这个问题时,这可能是一个问题。 When I tested the given link in safari Version 12.0.2 it seems to work as intendant 当我测试safari版本12.0.2中的给定链接时,它似乎是作为intendant工作
一种解决方法是使用按钮上的 onload 属性,创建一个添加侦听器的函数,然后在 window.onpopstate 和 window.onload 上调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.