jQuery Unload Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Event Handler

Definition

The browser unload event fires on window when the user leaves the page. In jQuery, bind it with .on("unload") (since 1.7) and simulate it with .trigger("unload").

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// Bind on window (required pattern)
$(window).on("unload", function () {
  // cleanup — keep it fast and simple
});

// Demo / tests only
$(window).trigger("unload");

// Removed shorthand (jQuery 3.0+)
// $(window).unload(fn);
CallWhat it does
UnloadLearn the jQuery unload event: bind with $(window).on(\

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select $(window) — not body or a random element.
Step 2Bind with .on("unload", handler).
Step 3Do only fast sync cleanup (flags, storage writes). Avoid UI dialogs.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Bind and Simulate Unload

Official pattern: bind on window, then trigger for a safe demo.

jQuery
$(window).on("unload", function () {
  $("#log").text("Handler for unload called.");
});

$("button").on("click", function () {
  $(window).trigger("unload");
});
Try It Yourself

How It Works

  1. Bind. Attach the handler to window.
  2. Click. The button triggers unload in code.
  3. Run. The handler updates the log.

Example 2 — Write a Cleanup Flag

Keep unload work tiny — e.g. store a flag in sessionStorage.

jQuery
$(window).on("unload", function () {
  sessionStorage.setItem("leftAt", String(Date.now()));
});

$("button").on("click", function () {
  $(window).trigger("unload");
  $("#log").text("leftAt=" + sessionStorage.getItem("leftAt"));
});
Try It Yourself

How It Works

  1. Bind. Register a short cleanup handler.
  2. Write. Save a timestamp to sessionStorage.
  3. Trigger. Simulate unload with a button.

Example 3 — Remove an Unload Handler

Use a named function so you can .off() it later.

jQuery
function onLeave() {
  $("#log").text("unload ran");
}

$(window).on("unload", onLeave);

$("#remove").on("click", function () {
  $(window).off("unload", onLeave);
  $("#log").text("Handler removed");
});
Try It Yourself

How It Works

  1. Name. Store the handler in onLeave.
  2. On. Bind it to window unload.
  3. Off. Pass the same reference to remove it.

Frequently Asked Questions

The unload event fires on the window when the user navigates away — link click, new URL, back/forward, tab close, or reload. Bind with $(window).on("unload", handler) since jQuery 1.7. Simulate with $(window).trigger("unload").
Official jQuery docs say unload handlers should be bound to the window object: $(window).on("unload", fn). That is the correct page-exit pattern.
unload runs while the page is leaving — mainly for cleanup. beforeunload fires earlier and can show a leave confirmation when the browser allows it. Use beforeunload only for unsaved-change warnings.
No. You cannot cancel unload with event.preventDefault(). For confirmations, use beforeunload and follow current browser prompt rules.
Same browser event. .unload(handler) was the shorthand removed in jQuery 3.0. .on("unload", handler) is the supported API. See the .unload() tutorial for migration.
Prefer pagehide or visibilitychange for reliable session-end logic. unload is unreliable on some mobile browsers, can break the back/forward cache, and is often discouraged. Use it mainly for legacy code or official-doc demos.

Next: .unload() Method

See the removed shorthand and how to migrate it to .on("unload").

.unload() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful