Javascript 是否有在移动 Safari 中使用 onbeforeunload 的替代方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6205989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an alternative method to use onbeforeunload in mobile safari?
提问by RJ Herbert
Is there an alternative method to use instead of onbeforeunload in mobile-safari? I've noticed that Google was able to capture the onbeforeunload
event in mobile-safari. Has anyone figured out how they are doing so?
在 mobile-safari 中是否有替代方法可以代替 onbeforeunload 使用?我注意到 Google 能够onbeforeunload
在 mobile-safari 中捕获该事件。有没有人想过他们是如何做到这一点的?
Google was able to do so using their gmail client. Create a new message... type something in the text area... hit the browser back button. It pops up a warning message. I used an iPad to do my test.
谷歌能够使用他们的 gmail 客户端做到这一点。创建一条新消息...在文本区域中输入一些内容...点击浏览器后退按钮。它会弹出一条警告消息。我用 iPad 来做我的测试。
回答by rrostt
I would guess that they are using the History API. By listening to popstate
我猜他们正在使用 History API。通过听 popstate
window.addEventListener("popstate", function(e) { ... });
you can show a warning before allowing doing anything stupid.
您可以在允许做任何愚蠢的事情之前显示警告。