jQuery unload()Method
Examples
Display a prompt message when leaving the page:
$(window).unload(function(){
alert("Goodbye!");
});
alert("Goodbye!");
});
Try it Yourself »
Definition and Usage
In jQuery version 1.8, the unload() method wasdeprecated, and removed in version 3.0.
Firefox and Chrome block pop-ups, so there is no way to see the effect.
The unload event occurs when the user leaves the page.
The unload event is triggered in the following cases:
- 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. Be sure to test the method in all browsers before using it.
Syntax
$(selector).unload(function)
| Parameter | Description |
|---|---|
| function | Required. Specifies the function to run when the unload event is triggered. |
Other Extensions
jQuery Event Methods