dan*_*dan 42 javascript back-button mobile-safari browser-history ios
在history.pushStateiOS上调用Safari 后,当使用浏览器后退按钮进行更改时,不再可以使用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>
Run Code Online (Sandbox Code Playgroud)
你可以在这里尝试一下:goo.gl/faFW6o.
小智 1
这是因为 Safari 中的后退缓存。
您可以使用以下代码在按下后退按钮时强制重新加载。
window.onpageshow = function(e) { // e -> event
if (e.persisted) {
window.location.reload();
}
};
Run Code Online (Sandbox Code Playgroud)
此外,如果您使用 jQuery ...
$(window).bind("pageshow", function(e) { // e -> event
if (e.originalEvent.persisted) {
window.location.reload();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1878 次 |
| 最近记录: |