.unload()    **REMOVED** JQ Home  <<  Events  <<  .unload()

Unload event handler.

Description

The .unload() method is used to bind an event handler to The JavaScript unload event.

  • The unload event is passed to the window object whenever a user navigates away from a page for any of the following reasons:
    1. The user has clicked on an external link.
    2. The user has pressed the page reload button.
    3. The user has pressed the forward or back navigation buttons.
    4. The user has typed a new URL in the address bar.
    5. The user has closed the browser window.
    6. Whether the event is triggered often depends on the browser being used and can even vary in different versions of a particular browser.

This method was deprecated in jQuery 1.8 and removed in 3.0.

  • Use .trigger( "unload" ) instead of .unload().
  • Use .on( "unload", handler ) instead of .unload( handler ).

Syntax

Signature Description
.unload( handler(eventObject) )Bind an event handler to the unload JavaScript event.
.unload( [eventData ,] handler(eventObject) )Bind an event handler to the unload JavaScript event, optionally passing an object of data.

Parameters

Parameter Description Type
handler( eventObject )A function to execute each time the event is triggered.Function
eventDataAn object of data to pass to the event handler.Anything

Return

A jQuery object.

.unload( handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the unload JavaScript event.

  • This signature is a shortcut for .on('unload', handler).

The example below no longer works from jQuery 3.0 onwards and is just shown as an example for people using earlier versions.

As soon as we navigate away from the page the unload JavaScript event occurs. In the example below we show an alert message when this happens.



$(window).unload(function() {
  alert('The JavaScript unload event has been triggered.');
});

.unload( [eventData ,] handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the unload JavaScript event, optionally passing a map of data.

  • This signature is a shortcut for .on('unload', handler).

The example below no longer works from jQuery 3.0 onwards and is just shown as an example for people using earlier versions.

As soon as we navigate away from the page the unload JavaScript event occurs. In the example below we show an alert message when this happens.

When the unload JavaScript event occurs ,it fires off the alert({ param1: 'unload', param2: '2 ', param3: 'unload 2 **JavaScript event triggered** ' }, addText2);; code.

What we are doing here is passing across the event object to the function addText(event). The map we specify, in our case param1: 'unload', param2: '2 ', param3: 'unload 2 **JavaScript event triggered** ' } gets tagged onto the event.data property.

We then access these parameters in the function via event.data.paramN and use the passed parameters for our appended data. As mentioned in the description above the code working depends on the browser being and can even vary in different versions of a particular browser.



$(window).unload(function() {
  alert({ param1: 'unload', param2: '2 ', 
               param3: 'unload 2 **JavaScript event triggered**  ' }, addText);
  
  function addText(event) {
    $(event.data.param1).append( event.data.param2 + ''<code>' + event.data.param3+ ''</code><br>');
    }
  });

Related Tutorials

jQuery Advanced Tutorials - Lesson 1 - Browser & Loading Events