JQuery unload()
JQuery unload()
is a method that allows you to perform certain actions when a page is unloaded or refreshed. It is often used to save important data or perform clean up actions before a user leaves the page.
Syntax
The syntax for using unload()
in JQuery is as follows:
$(window).unload(function() {
// code to be executed when the page is unloaded
});
Use
unload()
is useful when you need to perform certain actions when the user is leaving your page. For example, you may want to save form data or other user inputs before the page is unloaded. unload()
gives you an opportunity to run code before the user leaves the page.
Example
$(window).unload(function() {
// Save form data to server
$.post("/save-data", $("#my-form").serialize());
});