jQuery Event - unload Property

Example

A message box is displayed when the user clicks a link to leave this page:

$(window).unload(function(){
  alert("Goodbye!");
});

Try It Yourself

Definition and Usage

The unload event occurs when the user leaves the page.

Specifically, the unload event is triggered in the following situations:

  • Clicking a link to leave the page
  • A new URL was entered in the address bar
  • Using the forward or backward buttons
  • Close the browser
  • Reload the page

The unload() method binds an event handler to the unload event.

The unload() method is only applicable to the window object.

Syntax

event.unload(function)
Parameter Description
function Required. Specifies the function to be executed when the unload event is triggered.