尝试检测浏览器关闭事件 [英] Trying to detect browser close event
本文介绍了尝试检测浏览器关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过很多方法,通过的jQuery或JavaScript来检测浏览器关闭事件。但不幸的是,我一直没能检测到接近。在 onbeforeunload
和 onunload的
方法也无法正常工作。
I have tried many methods to detect browser close event through jQuery or JavaScript. But, unfortunately, I have not been able to detect the close. The onbeforeunload
and onunload
methods are also not working.
如何检测窗口关闭
,卸载
或 beforeunload
事件?
How do I detect the window close
, unload
, or beforeunload
events?
推荐答案
你有没有试过这种code?
Have you tried this code?
window.onbeforeunload = function (event) {
var message = 'Important: Please click on \'Save\' button to leave this page.';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
};
$(function () {
$("a").not('#lnkLogOut').click(function () {
window.onbeforeunload = null;
});
$(".btn").click(function () {
window.onbeforeunload = null;
});
});
第二个功能是可选的,以避免同时点击 #lnkLogOut
和 .btn
元素提示。
还有一件事,自定义提示将无法工作在Firefox(甚至在最新的版本也可以)。有关它的更多详细信息,请访问这个线程。
One more thing, The custom Prompt will not work in Firefox (even in latest version also). For more details about it, please go to this thread.
这篇关于尝试检测浏览器关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文