.unload()
** REMOVED **S2C 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 thewindow
object whenever a user navigates away from a page for any of the following reasons:- The user has clicked on an external link.
- The user has pressed the page reload button.
- The user has pressed the forward or back navigation buttons.
- The user has typed a new URL in the address bar.
- The user has closed the browser window.
- 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 and is shown for completeness.
- 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 |
eventData | An object of data to pass to the event handler. | Anything |
Return
A jQuery
object.
.unload( [eventData ,] handler(eventObject) )
ExampleTop
- This signature is a shortcut for
.on('unload', handler)
.
Bind an event handler to the unload
JavaScript event, optionally passing a map of data.
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 Top
- This signature is a shortcut for
.on('unload', handler)
.
Bind an event handler to the unload
JavaScript event, optionally passing a map of data.
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