The .unload() method was jQuery’s shorthand for binding to the browser’s unload event — typically on window to run cleanup when a page closes. Deprecated in 1.8 and removed in 3.0, it appears often in legacy themes. This tutorial covers bind, trigger, and eventData forms from the official API, migration to .on(), modern lifecycle alternatives, and five try-it examples.
01
Bind
Handler
02
Trigger
No args
03
eventData
Since 1.4.3
04
→ .on()
Migrate
05
Removed
jQuery 3.0
06
pagehide
Modern
Fundamentals
Introduction
Before single-page apps and modern lifecycle APIs, developers attached handlers to the browser unload event to save drafts, flush analytics, or tear down widgets when the user navigated away. jQuery wrapped that pattern in a one-line shorthand: $(window).unload(handler).
Available since jQuery 1.0, .unload() accepted a handler function, optional eventData (since 1.4.3), or no arguments to programmatically trigger bound handlers. It was deprecated in 1.8 alongside .load() and .error() event shorthands, then removed entirely in jQuery 3.0.
Do not use .unload() in new code. Replace it with .on("unload", handler) when maintaining legacy behavior, or prefer pagehide and visibilitychange for reliable page-lifecycle handling. Browse the Event Handler hub for modern .on() and .off() tutorials.
Concept
Understanding the .unload() Method
.unload( handler ) attached a function to the unload event on each matched element. On window, the handler ran when the document or a child resource was being unloaded — closing the tab, navigating away, or refreshing. .unload() with no arguments called every bound unload handler without a native browser navigation.
Like other legacy shorthands, .unload() was sugar over .bind("unload", handler) and .trigger("unload"). It could not delegate events and shared its name with unrelated Ajax APIs, which contributed to its removal.
⚠️
Removed in jQuery 3.0
$(window).unload(fn) throws or is undefined in jQuery 3.x+. Try-it labs 1–3 use jQuery 2.2.4 to demonstrate the legacy API accurately. Labs 4–5 use jQuery 3.7 with .on() and modern lifecycle events.
Foundation
📝 Syntax
Three signatures from the official jQuery API (deprecated 1.8, removed 3.0):
1. Bind handler — .unload( handler )since 1.0
jQuery
.unload( handler )
handler — function executed each time the unload event fires.
Most commonly used on $(window).
2. Bind with eventData — .unload( [eventData], handler )since 1.4.3
Handler binding forms return the collection; trigger form returns after handlers run.
Cheat Sheet
⚡ Quick Reference
Goal
Legacy (1.x–2.x)
Modern (3.x+)
Bind unload handler
$(window).unload(fn)
$(window).on("unload", fn)
Bind with data
$(window).unload(data, fn)
$(window).on("unload", data, fn)
Trigger handlers
$(window).unload()
$(window).trigger("unload")
Remove handler
$(window).unbind("unload", fn)
$(window).off("unload", fn)
Reliable page exit
$(window).on("pagehide", fn)
Tab hidden / visible
$(document).on("visibilitychange", fn)
Compare
📋 .unload() vs .on("unload") vs pagehide vs beforeunload
Four page-lifecycle approaches — know which API fits maintenance vs new development.
.unload()
removed 3.0
Legacy jQuery shorthand — read-only in old codebases using jQuery 1.x–2.x
.on("unload")
jQuery 3+
Direct migration path — same browser event, supported in modern jQuery
pagehide
recommended
Reliable page exit signal — bfcache-friendly, preferred over unload in new web apps
beforeunload
confirm leave
Show “unsaved changes” dialog — not for general cleanup; user-gesture rules apply
Hands-On
Examples Gallery
Examples 1–3 follow the official jQuery API signatures for .unload(). Examples 4–5 cover migration to .on() and modern lifecycle events. Try-it labs 1–3 load jQuery 2.2.4; labs 4–5 use jQuery 3.7.
📚 Official API Examples
Three signatures from api.jquery.com/unload-shorthand/.
Example 1 — Bind an Unload Handler on window
Run cleanup logic when the page unloads — the most common legacy pattern.
Handler attached with .unload(fn)
Try-it simulates with .trigger("unload") — real unload fires on navigation
sessionStorage updated on simulated unload
How It Works
.unload(handler) registers on jQuery’s event system for the native unload event. In production, the handler runs when the user leaves the page. In try-it demos, use .trigger("unload") to test without navigating away.
Example 2 — Bind With eventData (since 1.4.3)
Pass an object that becomes event.data inside the handler.
The no-argument form is shorthand for .trigger("unload"). It does not navigate away — it only invokes jQuery-bound handlers, which makes it useful for unit tests and interactive demos.
📈 Migration & Modern Alternatives
Replace legacy shorthand in jQuery 3+ and prefer reliable lifecycle events.
Example 4 — Migrate to .on("unload") in jQuery 3+
One-to-one replacement that works in current jQuery versions.
jQuery
// Before (jQuery 2.x)
// $( window ).unload( cleanup );
// After (jQuery 3.x+)
$( window ).on( "unload", cleanup );
function cleanup() {
localStorage.setItem( "draftSaved", "true" );
}
// Remove when done
$( window ).off( "unload", cleanup );
.on("unload", cleanup) binds in jQuery 3.7
.trigger("unload") runs cleanup
.off("unload", cleanup) removes handler
Minimal search-and-replace migration from .unload()
How It Works
Argument order and semantics match other events on .on(). Use .off("unload", cleanup) with the same function reference to unbind — the modern equivalent of .unbind("unload", cleanup).
Example 5 — Prefer pagehide for Reliable Cleanup
Modern browsers recommend pagehide over unload for session-end logic.
jQuery
$( window ).on( "pagehide", function( event ) {
// Runs when page enters bfcache or unloads — more reliable than unload
if ( event.originalEvent.persisted ) {
console.log( "Page cached — lightweight pause" );
} else {
console.log( "Page leaving — run cleanup" );
}
});
pagehide fires more reliably than unload
event.originalEvent.persisted === true when bfcache stores page
Recommended for analytics flush and session cleanup in new apps
How It Works
The native unload event is deprecated in browser standards and harms back/forward cache performance. When rewriting legacy .unload() code, evaluate whether pagehide or visibilitychange better matches your use case.
Applications
🚀 Common Use Cases (Legacy)
Analytics flush — send a beacon when the user navigates away (prefer pagehide today).
Draft autosave — persist form state on page exit (consider visibilitychange for tab switches).
Widget teardown — destroy plugins and timers when leaving a single-page section.
Session logging — record last-visit timestamps in sessionStorage.
Legacy maintenance — read old .unload() calls in jQuery 1.x/2.x themes during upgrades.
Migration audits — grep for .unload( and replace with .on("unload" or lifecycle alternatives.
🧠 How .unload() Bound Page-Exit Handlers
1
Select target element
Usually $(window) — the document window fires unload on navigation.
Target
2
Register handler
.unload(fn) internally calls .bind("unload", fn) on the collection.
Bind
3
Browser fires unload
On tab close, refresh, or external navigation — unreliable on mobile and with bfcache.
Event
4
🔄
Handler runs (or migrate)
Cleanup executes — in jQuery 3+, use .on("pagehide") for new lifecycle code.
Important
📝 Notes
Deprecated in jQuery 1.8; removed in jQuery 3.0 alongside .load() and .error() event shorthands.
Conflicted with Ajax .load(url) — a major reason for removal.
The native unload event is also discouraged by browser vendors.
Long-running unload handlers block navigation and hurt user experience.
Use beforeunload only for unsaved-change confirmation dialogs.
Try-it labs 1–3 require jQuery 2.2.4 to call .unload() directly.
Search legacy code: .unload(, bind("unload", and onunload=.
Compatibility
Browser Support
.unload() existed in jQuery 1.0 through 2.x and was removed in 3.0+. The underlying browser unload event still exists but is unreliable — mobile browsers, bfcache, and tab discarding may skip it. Use .on('unload') only when mirroring legacy behavior; prefer pagehide and visibilitychange for new lifecycle code in all modern browsers.
✓ Removed 3.0
jQuery .unload()
Legacy page-exit shorthand for jQuery 1.x and 2.x. Migrate to .on('unload') in jQuery 3+ or pagehide/visibilitychange for reliable session-end handling across current browsers.
LegacyjQuery 1.x–2.x only
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
.unload()Removed
Bottom line: Read and migrate legacy .unload() code — do not use in jQuery 3.x or new projects.
Wrap Up
Conclusion
The jQuery .unload() method was shorthand for binding to or triggering the browser unload event. It supported handler functions, optional eventData, and a no-argument trigger form — all documented on the official jQuery API page.
Because it was removed in jQuery 3.0, replace $(window).unload(fn) with $(window).on("unload", fn) during upgrades, and evaluate pagehide for reliable cleanup in new applications. Next up: apply modern .on("click") in the Click event tutorial.
Replace .unload(fn) with .on("unload", fn) when upgrading to jQuery 3+
Prefer pagehide or visibilitychange for new lifecycle handlers
Keep unload/pagehide handlers fast — avoid blocking network calls
Use .trigger("unload") in tests instead of real navigation
Pair cleanup bind with .off() when handlers are temporary
❌ Don’t
Call .unload() in jQuery 3.x — it no longer exists
Confuse event .load() with Ajax .load(url)
Rely on unload for critical saves on mobile browsers
Run heavy synchronous work inside unload handlers
Use beforeunload for general cleanup — it is for leave confirmations
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .unload()
Legacy page-exit shorthand — removed in jQuery 3.0, unreliable in modern browsers.
6
Core concepts
fn01
Bind
Handler
API
()02
Trigger
No args
Fire
{ }03
Data
1.4.3+
eventData
3.004
Removed
Not 3.x
Breaking
→05
.on()
Migrate
Replace
ph06
pagehide
Modern
Better
❓ Frequently Asked Questions
.unload(handler) binds a function to the browser unload event on matched elements — typically $(window).unload(fn). .unload() with no arguments triggers unload handlers. It was a shorthand for .bind('unload', handler) or .trigger('unload'). Removed in jQuery 3.0.
No. .unload() was deprecated in jQuery 1.8 and removed entirely in jQuery 3.0 because it conflicted with Ajax .load() and had API limitations. Use $(window).on('unload', handler) to bind and $(window).trigger('unload') to trigger instead.
They attach the same browser unload event when using jQuery 1.x–2.x. .unload() was sugar syntax removed in 3.0. .on('unload', fn) is the supported replacement in jQuery 3.x and 4.x and supports eventData, namespaces, and delegation like any other event.
Generally no. Browsers treat unload as unreliable — it breaks back/forward cache and may not fire on mobile tab switches. Prefer pagehide, visibilitychange, or beforeunload only when you need to warn users about unsaved changes.
jQuery 3.0 removed event shorthands that shared names with other APIs — .load() conflicted with Ajax .load(), and .error() conflicted with window.onerror. .unbind() had no naming collision, so it remains as deprecated but callable in jQuery 3.x.
Did you know?
jQuery 3.0 removed three event shorthands — .load(), .unload(), and .error() — because their names collided with other jQuery methods or DOM globals. .unbind() survived because it had no naming conflict, only a deprecation notice. When auditing old code, grep for all three removed shorthands plus their .bind("event") equivalents.