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);
Always bind on window, keep the handler tiny, and test real navigation separately from .trigger().
Select $(window) — not body or a random element.
Bind with .on("unload", handler).
Do only fast sync cleanup (flags, storage writes). Avoid UI dialogs.
For labs, call .trigger("unload") to run the handler without leaving.
For new apps, also consider pagehide as a more reliable exit signal.
Piece
Required?
Role
"unload"
Yes
Event type string for .on() / .trigger()
handler
Yes (bind)
function(event) — runs when unload fires
eventData
Optional
Extra 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
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.
Code
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");
});
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");
});
Bind. Use pagehide instead of (or alongside) unload.
Why. Works better with back/forward cache and many mobile browsers.
Demo. Trigger pagehide the same way in a lab.
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.
Compatibility
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.
Variesbrowser event
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
unloadLegacy
Bottom line: Fine for learning and legacy maintenance. For new apps, prefer pagehide / visibilitychange for exit cleanup.
Analysis
Performance Notes
Approach
Cost
Best for
Tiny sync cleanup
OK
Flags, storage writes
Network / heavy work on unload
Risky
Often aborted — avoid
pagehide cleanup
Better
Modern session-end logic
The page is disappearing — keep handlers short. Do not start Ajax or long loops; they may never finish.
Remember
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.