提示退出浏览器并关闭选项卡



我尝试了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;
}
);
})

在FireFox和Chrome中,您至少需要与页面进行一次交互。由于此功能是为了避免丢失用户输入的数据,因此如果页面上没有发生任何操作,则用户没有输入任何数据。

这是因为onbeforeunload是附加在窗口对象上的,当你卸载主页时,这个事件不会触发,除非你把焦点放在这个iframe上。

<script type="text/javascript">
window.onbeforeunload = function () {
return "Did you save your stuff?"
}

参考https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

最新更新