当用户在 Safari/Chrome 中关闭窗口时,我可以弹出确认对话框吗?
在 IE 和 FF 中,我可以将事件处理程序附加到 onBeforeUnload,并通过将字符串传递给事件的属性,用户将看到一个对话框,询问他是否要继续“卸载”(关闭窗口或导航离开)。
Safari 和 Chrome 不支持 onBeforeUnload,onUnload 似乎已经来不及了。 有没有办法在 Safari/Chrome 中复制相同的功能?
注意:我并不是想把用户当作人质。 我知道这很烦人和不酷。 事实上,我的网站竭尽全力让用户自由走动,并在他们回来时将所有内容放回原位。 然而,我在 IFrame 内托管其他网站,有时这些网站决定摆脱我并接管浏览器,这是我试图避免的。
谢谢!
In IE and FF, i can attach an event handler to onBeforeUnload, and by passing a string to a property of the event, the user will see a dialog asking him whether he wants to continue with the "unloading" (either closing the window or navigating away).
Safari and Chrome don't support onBeforeUnload, and onUnload seems to be too late.
Is there some way to replicate the same functionality in Safari/Chrome?
NOTE: I'm not trying to keep the user hostage. I know this is nagging and un-cool. In fact, my site goes to great lengths to let the user go freely, and have everything in its place when they come back.
However, I am hosting other sites inside IFrames, and sometimes these decide to get rid of me and take over the browser, which is what I'm trying to avoid.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这在 Chrome 和 Safari 中都非常适合我:
当我尝试关闭窗口时,我会收到提示。
This works perfectly for me in both Chrome and Safari:
When I try to close the window, I get the prompt.
StackOverflow 本身使用
onbeforeunload
,它在 Safari 中对我来说工作得很好:StackOverflow itself uses
onbeforeunload
, and it works fine for me in Safari:我在 chrome 中检查了它,使用它似乎工作正常:
I checked it in chrome and it seems to be working fine using this:
这个问题在另一个较新的 StackOverFlow 问题中有更详细的介绍:
在 Chrome 和 IE 中使用 body 元素设置 onbeforeunload jQuery
This question is covered in slightly more detail in another newer StackOverFlow question:
Setting onbeforeunload on body element in Chrome and IE using jQuery