Use jQuery to replace <body onunload="GUnload()">

When using Google maps it’s important to use GUnload on the page’s unload event. Fine when you can access the body tag directly…. But if you have the Google map functionality wrapped inside an ascx you need a different approach.

After a big of digging I found how to use jQuery to “catch” the unload event. In case anyone else needs it:

$(window).unload( function () { GUnload(); } );

This entry was posted by nerdboy on Monday, January 26th, 2009 at 5:03 pm and is filed under Javascript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below, or trackback from your own site.

Show/Hide Comments (13)

  • http://www.dop.nu Albert

    Thanks, I needed this.

  • Jesse

    cheers bol, just what i needed

  • CyberGhost

    Thanks a lot. This is just what I was looking for :)

  • Bob

    Thanks!

  • Oscuro

    Thanks, exactly what I was looking for! :)

  • Nigel

    Does
    $(document).unload( function () { GUnload(); } );
    do the same trick?

  • MiLL

    Спасибо :)

  • Jarrod

    Thanks for the tip, I was trying $(“body”). You could shorten it to this as well:

    $(window).unload( GUnload );

    Pity about google maps v3 memory leaks and no GUnload.

  • http://www.gamerzorg.com LSaridina

    Thanks this will solve my stats problem. I can’t detect when user leave my pages before now. Thanks!

  • vpgugr

    Cool! Thank you

  • Vitaly Grinko

    Perfect, thank you!

  • Raffaello Camoriano

    Thank you, concise and effective. Great tip!

  • Eliprodigy

    try this
    window.onbeforeunload =
    function () {
    return “Are You Sure?”;
    };