如果 OnBeforeUnload 接受对话框,可以运行代码吗?
我想控制人们离开我的应用程序的容易程度。现在,我知道我不能完全做到;这就是 cron 作业的用途。但我想抓住尽可能多的案例。
我想,我可以使用 onBeforeUnload
显示对话框,警告他们要离开,并要求他们单击“退出”按钮。
但后来我发现,当调用 onBeforeUnload
时,可以运行同步 AJAX(呵呵)调用;据我了解,它是同步的好处是浏览器将等待它完成然后再继续。这样,我可以告诉服务器,当他们关闭窗口时我正在注销。由于它位于 onBeforeUnload
中,而不是 onUnload
,因此所有数据仍然存在,并且将发送调用。
我的问题是,是否可以将这些结合起来?我想要一个弹出窗口警告他们要离开,然后如果他们继续想离开,请发送他们实际上要离开的消息。
按照我的理解,我不能这样做;我可以这样做:
- 仅发送消息 仅
- 建立对话框
- 发送消息并建立对话框
...但我无法发送消息条件是他们是否接受对话框< /em>,对吧?我的理解是,当他们单击“确定,我想离开”时,它会立即触发 onUnload
,到那时就为时已晚了。上面的 #3 没有用,因为我不想发送他们正在注销的消息,然后让他们取消对话框并留在页面上。
我可以一边吃蛋糕一边吃吗?我可以询问他们是否想离开,如果想离开,请发送消息吗?
I want to control how easily people can leave my app. Now, I know I can't do it completely; that's what cron jobs are for. But I'd like to catch as many cases as possible.
I thought, I could use onBeforeUnload
to display the dialog box, warning them about leaving and asking them to please click the Quit button instead.
But then I found out that it could be possible to run a synchronous AJAX (heh) call when onBeforeUnload
is called; the benefit of it being synchronous is that the browser will wait for it to finish before continuing, as I understand it. That way, I could tell the server that I'm logging off as they close the window. And because it's in onBeforeUnload
and not onUnload
, all of the data is still extant and the call will be sent.
My question is, is it possible to combine these? I would like a popup warning them about leaving, and then IF they continue to want to leave, send the message that they are in fact leaving.
The way I understand it, I can't do this; I can do:
- Send the message only
- Put up the dialog only
- Send the message and put up the dialog
... but I can't send the message conditionally on if they accepted the dialog, right? My understanding is that when they click "OK, I want to leave", it immediately fires onUnload
, and by that point it's too late. And #3 above isn't useful because I don't want to send the message that they're logging off, and then have them cancel the dialog and stay on the page.
Can I have my cake and eat it too? Can I ask them if they want to leave and, if so, send the message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中已经指出的,
onbeforeunload
在 Opera 中不起作用。但是,我在 Firefox (3.6.3/Linux) 和 Chromium (5.0.375.70) 中尝试了以下代码,它或多或少有效。但是,当关闭 Chromium 窗口或选项卡时,会显示确认框,但即使单击“确定”,窗口/选项卡也会在一秒钟内关闭。在onbeforeunload
期间,同步 XMLHttpRequest 似乎存在超时。更新 - 只是为了完整起见,因为我忘记包含
wait.php
,它仅保持连接 10 秒:As I already stated in a comment,
onbeforeunload
doesn't function in Opera. However, I tried the following code in Firefox (3.6.3/Linux) and Chromium (5.0.375.70) and it worked more or less. However, when closing the Chromium window or tab, the confirmation box is shown, but within a second the window/tab is closed, even when clickingOk
. There seems to be a timeout for synchronous XMLHttpRequests duringonbeforeunload
.Update – Just for the sake of completeness, as I forgot to include
wait.php
, it just holds the connection for 10 seconds: