在使用1.10.1的jQuery的过程中,当调用jQuery中的unload事件的时候在chrome、Firefox、Opera浏览器中当进行关闭或刷新操作的时候都不能弹出自定义的警告信息。、
$(window).unload( function () { alert("Bye now!"); } );
查阅了相关信息后,有的说这个方法已经在jQuery1.8.1中废弃了点击打开链接,但是官方文档上并没有显示的说明这个方法已经废弃,官方对这个方法的解释是由于浏览器对各个方法的执行方式的不同,可能到时浏览器在执行这个方法的时候不会出现想要的结果。
引用:
The exact handling of the unload
event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers, and contrasted with the proprietary beforeunload
event.
在这里提出两种解决方法:
$(window).on("beforeunload",function() {
return "你确定要关闭吗?";
});
$(window).bind('beforeunload',function() {
return "你确定要关闭吗?";
});
具体的on方法和bind方法,可以参考具体的帮助文档