jQuery Unload Event

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is the unload Event?

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").

Remember
// 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);
NeedUse
Bind unload$(window).on("unload", fn)
Trigger for demos$(window).trigger("unload")
Remove handler$(window).off("unload", fn)
Legacy shorthand.unload() ✗ removed in 3.0
Modern cleanuppagehide / visibilitychange

How to Use the unload Event

Always bind on window, keep the handler tiny, and test real navigation separately from .trigger().

  1. Select $(window) — not body or a random element.
  2. Bind with .on("unload", handler).
  3. Do only fast sync cleanup (flags, storage writes). Avoid UI dialogs.
  4. For labs, call .trigger("unload") to run the handler without leaving.
  5. For new apps, also consider pagehide as a more reliable exit signal.
PieceRequired?Role
"unload"YesEvent type string for .on() / .trigger()
handlerYes (bind)function(event) — runs when unload fires
eventDataOptionalExtra data passed into the handler (jQuery 1.7+)
Target—Bind on window

Pseudocode

Pseudocode
bind:
  $(window).on("unload", handler)

when user leaves the page (or trigger("unload") runs):
  call handler(event)
  // alert / confirm / prompt usually ignored
  // preventDefault cannot cancel navigation

cleanup tip:
  keep handler short
  write flags/storage if needed
  prefer pagehide for modern apps

Cheat Sheet

Keep these facts handy when handling page exit.

TopicDetail
Bind API.on("unload") since jQuery 1.7
Target$(window)
TriggersLeave link, URL change, back/forward, close, reload
CancelNot possible via preventDefault()
Dialogsalert / confirm / prompt usually ignored
vs beforeunloadbeforeunload = confirm leave · unload = cleanup
Shorthand.unload() removed in jQuery 3.0
Modern altpagehide, visibilitychange

Live Preview

Bind an unload handler on window, then simulate it with .trigger("unload") — safe for demos without leaving the page.

Demo target: window. Trigger runs your handler without navigating away.

Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Labs use .trigger("unload") so you can stay on the page.

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.
  4. Stay. The page does not navigate away.

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.
  4. Read. Show the stored value in the log.

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.
  4. Test. Triggering unload afterward does nothing.

Example 4 — Pass eventData

jQuery can pass extra data into the unload handler.

jQuery
$(window).on("unload", { reason: "demo" }, function (event) {
  $("#log").text("reason=" + event.data.reason);
});

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

How It Works

  1. Data. Pass a plain object as the middle argument.
  2. Event. Read it as event.data in the handler.
  3. Trigger. Fire unload to see the value.
  4. Use. Handy for labels in demos and shared helpers.

Example 5 — Prefer pagehide for Cleanup

Modern guidance: listen for pagehide when you need reliable exit cleanup.

jQuery
$(window).on("pagehide", function () {
  $("#log").text("pagehide — better for modern cleanup");
});

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

How It Works

  1. Bind. Use pagehide instead of (or alongside) unload.
  2. Why. Works better with back/forward cache and many mobile browsers.
  3. Demo. Trigger pagehide the same way in a lab.
  4. Still. Know unload for legacy docs and old code.

Edge Cases & Pitfalls

Unload behavior varies by browser — design for the worst case.

cancel

Cannot stop navigation

preventDefault() does nothing useful on unload. Use beforeunload for leave prompts.

dialogs

Alerts are usually ignored

Most browsers ignore alert, confirm, and prompt inside unload handlers.

mobile

Unreliable on some mobile browsers

Prefer pagehide / visibilitychange for analytics and session cleanup.

shorthand

.unload() is gone in jQuery 3

Use .on("unload") / .trigger("unload"). See the .unload() page.

Browser Support

The browser unload event is long-standing, but behavior differs by browser (especially mobile and back/forward cache). jQuery binds it with .on("unload") since 1.7. Prefer pagehide for modern cleanup.

✓ Legacy event · use carefully

jQuery unload event

Available wherever window unload exists, but reliability varies. Test real navigation on every browser you support.

Varies browser event
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
unload Legacy

Bottom line: Fine for learning and legacy maintenance. For new apps, prefer pagehide / visibilitychange for exit cleanup.

Performance Notes

ApproachCostBest for
Tiny sync cleanupOKFlags, storage writes
Network / heavy work on unloadRiskyOften aborted — avoid
pagehide cleanupBetterModern session-end logic

The page is disappearing — keep handlers short. Do not start Ajax or long loops; they may never finish.

Key Takeaways

  • Bind: $(window).on("unload", fn) — always on window.
  • Demo: use .trigger("unload") without leaving the page.
  • Limits: cannot cancel; dialogs usually ignored.
  • Modern: prefer pagehide for reliable cleanup.

One line: bind unload on window for page-exit cleanup — keep it tiny, and prefer pagehide in new apps.

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() tutorial →

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