如何知道用户是否在 Javascript onbeforeunload 对话框上单击“取消”?
当有人试图在未保存工作的情况下离开特定页面时,我会弹出一个对话框。我使用 Javascript 的 onbeforeunload 事件,效果很好。
现在,当用户单击出现的对话框中的“取消”(表示他们不想离开该页面)时,我想运行一些 Javascript 代码。
这可能吗?我也在使用 jQuery,那么我可以绑定像 beforeunloadcancel 这样的事件吗?
更新:这个想法是在用户选择取消时实际保存并引导他们到不同的网页
I am popping up a dialog box when someone tries to navigate away from a particular page without having saved their work. I use Javascript's onbeforeunload event, works great.
Now I want to run some Javascript code when the user clicks "Cancel" on the dialog that comes up (saying they don't want to navigate away from the page).
Is this possible? I'm using jQuery as well, so is there maybe an event like beforeunloadcancel I can bind to?
UPDATE: The idea is to actually save and direct users to a different webpage if they chose cancel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以这样做:
第一个
setTimeout
方法中的代码有 1ms 的延迟。这只是将该函数添加到UI 队列
中。由于setTimeout
异步运行,Javascript 解释器将通过直接调用return
语句继续,从而触发浏览器模态对话框
。这将阻塞UI 队列
,并且第一个setTimeout
中的代码不会执行,直到模式关闭。如果用户按下“取消”,它将触发另一个 setTimeout,该事件会在大约一秒内触发。如果用户确认“确定”,用户将重定向,并且第二个 setTimeout 永远不会被触发。示例: http://www.jsfiddle.net/NdyGJ/2/
You can do it like this:
The code within the first
setTimeout
method has a delay of 1ms. This is just to add the function into theUI queue
. SincesetTimeout
runs asynchronously the Javascript interpreter will continue by directly calling thereturn
statement, which in turn triggers the browsersmodal dialog
. This will block theUI queue
and the code from the firstsetTimeout
is not executed, until the modal is closed. If the user pressed cancel, it will trigger another setTimeout which fires in about one second. If the user confirmed with ok, the user will redirect and the second setTimeout is never fired.example: http://www.jsfiddle.net/NdyGJ/2/
我知道这个问题现在已经很老了,但是如果有人仍然遇到问题,我找到了一个似乎对我有用的解决方案,
基本上
unload
事件是在beforeunload 之后触发的事件。我们可以用它来取消在
beforeunload
事件中创建的超时,修改 jAndy 的答案:编辑: jsfiddle 在这里
I know this question is old now, but in case anyone is still having issues with this, I have found a solution that seems to work for me,
Basically the
unload
event is fired after thebeforeunload
event. We can use this to cancel a timeout created in thebeforeunload
event, modifying jAndy's answer:EDIT: jsfiddle here
不可能。也许有人会证明我错了......你想运行什么代码?您想在他们单击“取消”时自动保存吗?这听起来违反直觉。如果您还没有自动保存,我认为当他们点击“取消”时自动保存没有什么意义。也许您可以突出显示 onbeforeunload 处理程序中的保存按钮,以便用户在离开之前看到他们需要执行的操作。
Not possible. Maybe someone will prove me wrong... What code do you want to run? Do you want to auto-save when they click cancel? That sounds counter-intuitive. If you don't already auto-save, I think it makes little sense to auto-save when they hit "Cancel". Maybe you could highlight the save button in your onbeforeunload handler so the user sees what they need to do before navigating away.
我认为这是不可能的,但只是尝试了这个想法并且它有效(尽管它是一些黑客行为并且可能无法在所有浏览器中都以相同的方式工作):
I didn't think it was possible, but just tried this idea and it works (although it is some what of a hack and may not work the same in all browsers):
另一种变化
第一个 setTimeout 等待用户响应浏览器的离开/取消弹出窗口。第二个 setTimeout 等待 1 秒,然后仅在用户取消时调用 CancelSelected。否则页面会被卸载并且setTimeout会丢失。
Another variation
The first setTimeout waits for the user to respond to the browser's Leave/Cancel popup. The second setTimeout waits 1 second, and then CancelSelected is only called if the user cancels. Otherwise the page is unloaded and the setTimeout is lost.
使用此代码,如果用户在 IE8 中单击“取消”,也会出现默认导航对话框。
with this code also if user clicks on 'Cancel' in IE8 the default navigation dialog will appear.