当用户在Safari / Chrome中关闭窗口时,我可以弹出确认对话框吗? [英] Can I pop up a confirmation dialog when the user is closing the window in Safari/Chrome?

查看:141
本文介绍了当用户在Safari / Chrome中关闭窗口时,我可以弹出确认对话框吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IE和FF中,我可以将一个事件处理程序附加到onBeforeUnload上,并通过将一个字符串传递给事件属性,用户将看到一个对话框,询问他是否要继续卸载(要么关闭窗口或导航)



Safari和Chrome不支持onBeforeUnload,onUnload似乎太晚了。
是否有某种方式可以在Safari / Chrome中复制相同的功能?



注意:我并不试图让用户成为人质。我知道这是唠叨和冷静。事实上,我的网站竭尽全力让用户自由行动,并且在他们回来时拥有一切。
但是,我在IFrame内部托管了其他网站,有时这些网站决定摆脱我并接管浏览器,这正是我试图避免的。



谢谢!

解决方案



 < html>< body>< p>测试< / p> 
< script> window.onbeforeunload = function(){returnSure?; }< /脚本>
< / body>< / html>

当我尝试关闭窗口时,显示提示。


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!

解决方案

This works perfectly for me in both Chrome and Safari:

<html><body><p>Test</p>
<script>window.onbeforeunload = function() { return "Sure?"; }</script>
</body></html>

When I try to close the window, I get the prompt.

这篇关于当用户在Safari / Chrome中关闭窗口时,我可以弹出确认对话框吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆