jQuery .unload() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Legacy shorthand

What You’ll Learn

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

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.

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.

📝 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

jQuery
$( window ).unload( { app: "myApp" }, function( event ) {
  console.log( event.data.app );  // "myApp"
});

3. Trigger handlers — .unload() since 1.0

jQuery
$( window ).unload();  // fire bound unload handlers

Migration to .on() / .trigger()

jQuery
// Legacy (jQuery 1.x–2.x)
$( window ).unload( handler );
$( window ).unload( { app: "myApp" }, handler );
$( window ).unload();

// jQuery 3.x+ replacement
$( window ).on( "unload", handler );
$( window ).on( "unload", { app: "myApp" }, handler );
$( window ).trigger( "unload" );

// Modern lifecycle (recommended for new code)
$( window ).on( "pagehide", handler );
$( document ).on( "visibilitychange", function() {
  if ( document.visibilityState === "hidden" ) { /* cleanup */ }
});

Return value

  • Returns the jQuery object for chaining.
  • Handler binding forms return the collection; trigger form returns after handlers run.

⚡ Quick Reference

GoalLegacy (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)

📋 .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

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.

jQuery
$( window ).unload( function() {
  // Legacy cleanup — runs on page exit
  sessionStorage.setItem( "lastVisit", Date.now() );
});
Try It Yourself

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.

jQuery
$( window ).unload( { appId: "dashboard" }, function( event ) {
  console.log( "Cleaning up:", event.data.appId );
});
Try It Yourself

How It Works

jQuery copies the object into event.data at bind time. Useful when one handler function serves multiple widgets with different configuration objects.

Example 3 — Trigger Unload Handlers Programmatically

Call .unload() with no arguments to execute every bound handler.

jQuery
$( window ).unload( function() {
  $( "#log" ).append( "<p>Unload handler ran</p>" );
});

// Later — test or simulate page exit
$( window ).unload();
Try It Yourself

How It Works

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 );
Try It Yourself

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" );
  }
});
Try It Yourself

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.

🚀 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.

📝 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=.

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.

Legacy jQuery 1.x–2.x only
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() Removed

Bottom line: Read and migrate legacy .unload() code — do not use in jQuery 3.x or new projects.

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.

💡 Best Practices

✅ Do

  • 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

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
() 02

Trigger

No args

Fire
{ } 03

Data

1.4.3+

eventData
3.0 04

Removed

Not 3.x

Breaking
05

.on()

Migrate

Replace
ph 06

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.
$(window).unload(fn) becomes $(window).on('unload', fn). $(window).unload({ id: 1 }, fn) becomes $(window).on('unload', { id: 1 }, fn). Programmatic .unload() becomes .trigger('unload'). Consider pagehide or visibilitychange for reliable page-lifecycle code.
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.

Next: Click Event

Apply modern .on() and .off() to the click event in practice.

Click event 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.

6 people found this page helpful