Different behavior of jQuery.unload() in IE and Chrome

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

When working with jQuery .unload() function you might figure out that Internet Explorer and Chrome behave differently.
For example, assume you have defined following handler, which explicitly closes window, when user double-click some element.

  $("document").ready(function () {

 

        $("#el").dblclick(function () {

            window.close();

        });

  $(window).unload(function () {

    
      alert(“unload called”)   

 

    });

In a case of IE the unload method will be properly invoked. Unfortunately if you use Google Chrome you will figure out that unload() will not be called when Chrome hosted page is opened first time. After you open the same page second time the Chrome will properly invoke the unload function.
Open means something like:

window.showModalDialog(uri, window,
"resizable=yes,scrollbars=yes,status=no, dialogWidth:20px");

To workaround this issue you need to do following:

$("document").ready(function () {

    $("#el").dblclick(function () {

       $(window).unload();

       window.close();

});


Remarks: If you close the window by clicking on “X” (not by invoking window.close()) all will work properly.

Posted Sep 02 2011, 02:50 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.