jQuery bind beforeunload - not working with IE 9

时间秒杀一切 提交于 2020-01-05 07:56:58

问题


I have a piece of code i want fired when the page closes (basically, send a 'disconnected' message to the server. The execution should be fast enough for me not to have to cancel and restart the event. Further to that, it works perfectly in Chrome, Firefox and Safari, but not in IE9 on closing the tab. If i navigate to another page in IE9, my event fires. If i close the tab, it doesn't. I tried the following to bind my code:

jQuery(window).bind("beforeunload", function() { DoSomeWork(); });

i also tried replacing jQuery with $ like so:

$(window).bind("beforeunload", function() { DoSomeWork(); });

Still works in Chrome, but does not work in IE. Any suggestions?

I am using jquery 1.9.1 min (compressed production version).


回答1:


Try this

onbeforeunload = function() {
     return "Are you sure";
}

Tested in Chrome Version 26.0.1410.64 m Firefox Version 20.0.1 Internet Explorer Version 9.0

FIDDLE



来源:https://stackoverflow.com/questions/16380114/jquery-bind-beforeunload-not-working-with-ie-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!