jQuery unload() method
Example
Display a message when leaving the page:
$(window).unload(function(){
alert("Goodbye!");
});
Try it »
Definition and Usage
The unload() method was deprecated in jQuery version 1.8deprecated, and removed in version 3.0.
Firefox and Chrome block pop-ups, so you cannot see the effect.
The unload event occurs when the user leaves the page.
The unload event is triggered in the following situations:
- Clicking a link that leaves the page
- Typing a new URL in the address bar
- Using the Forward or Back button
- Closing the browser window
- Reloading the page
The unload() method specifies what happens when the unload event occurs.
The unload() method applies only to the window object.
**Note:** The unload event behaves differently in different browsers; make sure to test this method in all browsers before using it.
Syntax
$(
selector
).unload(
function
)
| Parameters | Description |
|---|---|
| function | Required. Specifies the function to run when the unload event is triggered. |
jQuery event methods