Search code examples
jqueryinternet-exploreronbeforeunload

jQuery bind beforeunload - not working with IE 9


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).


Solution

  • 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