jQuery API

.unload()

.unload( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "unload" JavaScript event.

  • version added: 1.0.unload( handler(eventObject) )

    handler(eventObject)A function to execute when the event is triggered.

  • version added: 1.4.3.unload( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

This method is a shortcut for .bind('unload', handler).

The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.

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.

Any unload event handler should be bound to the window object:

$(window).unload(function() {
  alert('Handler for .unload() called.');
});

After this code executes, the alert will be displayed whenever the browser leaves the current page. It is not possible to cancel the unload event with .preventDefault(). This event is available so that scripts can perform cleanup when the user leaves the page.

Example:

To display an alert when a page is unloaded:

$(window).unload( function () { alert("Bye now!"); } );

Support and Contributions

Need help with .unload() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .unload()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Anonymous

    Unload-Event isn’t executed in opera 10.52. Is it a Bug, or a Opera-Task.?

    • Fbfb

      kjhbuibmnin

    • Assad Safiullah

      I can’t even get this to work on Opera, Safari 5.0 (win) OR chrome. Have you tried on any of these browsers?

      • Atholon

        Me neither. I thought I had it working in Chrome for a while… Safari after version 4 it worked ok.

  • Chris Taylor

    For the .unload(), is there a way to capture the event that made the call? If so, do you know of any examples?

    Thank you,
    Chris

  • Chris Taylor

    For the .unload(), is there a way to capture the event that made the call? If so, do you know of any examples?

    Thank you,
    Chris

  • http://herberthamaral.com/ Herberth Amaral

    Is there a way to use unload with another kind of object/element but window when it no longer exists (like when a element is removed)?

  • Fguingon

    Would it ok to insert the window.onbeforeunload in document.ready () ?

  • spartan

    For chrome and safari, include the webkit hack script to make it work.

  • dan

    doesn’t work in IE6, i get an object is not null or undefined..

  • Princerobair

    When i start up my computer i get an annoying prompt about unload that I can only remove with ctrl ault del.

  • Laurent

    have you find a hack ?

  • Alexandru Sima20

    hello,
    does anyone no how to catch only the browser close event, not refreshing pages?!

  • Alexandru Sima20

    hello,
    does anyone no how to catch only the browser close event, not refreshing pages?!

  • arix

    Not run in firefox 3.6.1.3 with a popup

  • Wenliangbeyond

    我看不懂啊,all is english !

    • Ali

      ya!!!All is english do you have some

  • kat

    is there anyway to cancel the unload event?

  • Awysocki

    One item I found. in the UNLOAD event, to post/get something back to your site, use $.ajax() with the “ASYNC : false” set and it works in all browsers

  • Laurent

    have you find a hack ?