[英]Detect if user did NOT cancel on a Javascript onbeforeunload Dialog?
There's lots of questions on how to raise a dialog before a user leaves, and how to detect if the user decided not to leave. 关于如何在用户离开之前引发对话框以及如何检测用户是否决定离开的问题有很多问题。
javascript before leaving the page 离开页面前的javascript
Detecting whether user stayed after prompting onBeforeUnload 提示onBeforeUnload后检测用户是否留下
What if I want to do an action only if the user confirms that he wants to leave. 如果我仅在用户确认要离开时才想做一个动作,该怎么办。
But I don't want to run action() if the user cancels the leave dialog and ends up staying on the page. 但是,如果用户取消了离开对话框并最终停留在页面上,我就不想运行action()。
How can I detect confirm for onbeforeunload
JavaScript Dialog? 如何在
onbeforeunload
JavaScript对话框中检测确认?
Use the unload
event. 使用
unload
事件。 This event will be fired after they confirm the dialogue displayed because of the onbeforeunload event. 由于onbeforeunload事件,他们确认显示的对话后将触发此事件。
window.addEventListener("unload", function() {
console.log("Bye!");
});
Note that there are restrictions on what you can do in beforeunload
and unload
event handlers, to prevent malware that completely prevents you from closing the window/tab. 请注意,对于在
beforeunload
和unload
事件处理程序中可以执行的操作有一些限制,以防止恶意软件完全阻止您关闭窗口/选项卡。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.