Example

Alert a message when the user navigates away from the page:

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

Try it yourself »

Definition and Usage

The unload event occurs when the user navigates away from the page.

The unload event is triggered when:

The unload() method specifies what happens when a unload event occurs.

The unload() method should only be used on the window object.

Syntax

$(selector).unload(function)

Parameter Description
function Required. Specifies the function to run when the unload event is triggered.