【发布时间】:2021-11-25 14:02:23
【问题描述】:
我尝试了 onbeforeunload,因为它需要一次用户交互才能调用,有没有什么方法可以在没有用户交互的情况下调用事件,因为用户在打开应用程序选项卡后退出浏览器。 或任何其他不使用 onbeforeunload 的解决方案,阻止用户退出浏览器。
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;
};
$(document).ready(function(){
$(".label").click(function(){
alert('label');
window.onbeforeunload = null;
}
);
$(".new_cont").click(function(){
alert('new_cont');
window.onbeforeunload = null;
}
);
})
【问题讨论】:
-
这能回答你的问题吗? Detect browser or tab closing
标签: javascript c# jquery asp.net