jQuery .unload() Method

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

What Is .unload()?

.unload() was jQuery’s shorthand (since 1.0) for binding or triggering the browser unload event — almost always on window. It is gone in jQuery 3.x. Use .on("unload") / .trigger("unload"), or prefer pagehide for modern cleanup.

Remember
// Removed in jQuery 3.0 — only works in 1.x / 2.x
$(window).unload(function () {
  // page is leaving
});

$(window).unload(); // trigger

// Modern replacements
$(window).on("unload", handler);
$(window).trigger("unload");

// Often better for cleanup today
$(window).on("pagehide", handler);
NeedUse
New code (bind)$(window).on("unload", fn)
New code (trigger)$(window).trigger("unload")
Legacy bind$(window).unload(fn) ✗ removed 3.0
Legacy trigger$(window).unload() ✗ removed 3.0
Modern cleanup$(window).on("pagehide", fn)

How to Use .unload()

Recognize legacy calls, bind on window, and migrate to .on() / .trigger().

  1. In old code, bind with $(window).unload(handler) (jQuery 1.x / 2.x only).
  2. Optionally pass eventData: .unload(data, handler) → .on("unload", data, handler).
  3. Trigger with no-arg .unload() (legacy) or .trigger("unload") (preferred).
  4. Keep handlers light — browsers ignore alert() / confirm() inside unload.
  5. For new apps, prefer pagehide or visibilitychange over unload.
PieceRequired?Role
windowYes (typical)Standard target for page unload
handlerFor bind formfunction(event)
eventDataOptional (1.4.3+)Extra data as event.data
jQuery versionCriticalMethod missing in 3.x

Pseudocode

Pseudocode
legacy (jQuery 1.x / 2.x only):
  call $(window).unload(handler)
      → internally: $(window).on("unload", handler)

  call $(window).unload()
      → internally: $(window).trigger("unload")

when user leaves the page:
  fire unload on window
      for each registered handler:
          call handler(event)
          // cannot cancel with preventDefault()

to migrate:
  .unload(fn)       → .on("unload", fn)
  .unload(data, fn) → .on("unload", data, fn)
  .unload()         → .trigger("unload")

prefer today:
  .on("pagehide", fn) for cleanup

Cheat Sheet

Quick map from the removed shorthand to modern APIs.

TopicDetail
Legacy bind$(window).unload(handler) ✗ removed 3.0
Modern bind$(window).on("unload", handler)
Legacy trigger$(window).unload()
Modern trigger$(window).trigger("unload")
Unbind$(window).off("unload" [, handler])
Since / gone1.0 · eventData 1.4.3 · deprecated 1.8 · removed 3.0
Cancel?No — use beforeunload for leave prompts
Better altpagehide / visibilitychange

Live Preview

This site loads jQuery 3.x, so .unload() is missing. The demo uses the modern .on("unload") / .trigger("unload") replacement.

Simulation only — real unload fires on navigation. Legacy .unload() demos use jQuery 2.2.4 in Try It Yourself.

Result
Ready — pick an action and click Run.

jQuery Examples

Examples 1–3 use jQuery 2.2.4 so .unload() exists. Examples 4–5 show jQuery 3.x migration.

Example 1 — Basic $(window).unload(fn)

Legacy bind on window. Simulate with .trigger("unload") in the lab.

jQuery
$(window).unload(function () {
  $("#log").text("Unload handler ran");
});

// Demo only — real unload fires on navigation
$(window).trigger("unload");
Try It Yourself

How It Works

  1. Bind. .unload(fn) registers a window unload handler.
  2. Leave. Navigation, reload, or close can fire the event.
  3. Demo. .trigger("unload") runs handlers without leaving.
  4. Migrate. Swap to .on("unload", fn) for jQuery 3+.

Example 2 — .unload(eventData, handler)

Pass extra data (since 1.4.3). Modern form: .on("unload", data, fn).

jQuery
$(window).unload({ appId: "dashboard" }, function (event) {
  $("#log").text("Cleaning up: " + event.data.appId);
});

$(window).trigger("unload");
Try It Yourself

How It Works

  1. Data. The object becomes event.data in the handler.
  2. Bind. Legacy .unload(data, fn) attaches both.
  3. Trigger. The demo fires unload so you can see the label.
  4. Migrate. Use .on("unload", data, fn) in jQuery 3+.

Example 3 — No-arg .unload() Trigger

Fire bound handlers programmatically. Prefer .trigger("unload") today.

jQuery
$(window).unload(function () {
  $("#log").text("Handler ran via .unload()");
});

$("#fire").on("click", function () {
  $(window).unload(); // legacy trigger
});
Try It Yourself

How It Works

  1. Bind. A handler is registered with the shorthand.
  2. Click. No-arg .unload() triggers the event.
  3. Run. Bound handlers execute immediately.
  4. Migrate. Replace with .trigger("unload").

Example 4 — Migrate to .on("unload")

jQuery 3.x pattern: bind, trigger, and remove with .on / .off.

jQuery
function cleanup() {
  localStorage.setItem("draftSaved", "true");
  $("#log").text("cleanup ran — draftSaved=true");
}

$(window).on("unload", cleanup);
$(window).trigger("unload");
$(window).off("unload", cleanup);
Try It Yourself

How It Works

  1. On. .on("unload", cleanup) is the 3.x replacement.
  2. Trigger. .trigger("unload") replaces no-arg .unload().
  3. Off. Remove with the same function reference.
  4. Same event. Behavior matches the old shorthand API.

Example 5 — Prefer pagehide

Modern alternative for page-lifecycle cleanup (bfcache-friendly).

jQuery
$(window).on("pagehide", function (event) {
  var persisted = event.originalEvent && event.originalEvent.persisted;
  $("#log").text(
    persisted ? "pagehide — page cached (bfcache)" : "pagehide — page leaving"
  );
});
Try It Yourself

How It Works

  1. Listen. pagehide fires when the page is being hidden or unloaded.
  2. persisted. true means the page may enter the back/forward cache.
  3. Cleanup. Good place for beacons and session end logic.
  4. Why. More reliable than unload for modern browsers.

Edge Cases & Pitfalls

Watch for these when reading legacy unload code or migrating.

removed

Missing in jQuery 3.x

$.fn.unload is undefined — calling it throws. Migrate to .on("unload").

target

Bind on window

Page unload is delivered to window. Prefer $(window).on("unload", fn).

dialogs

alert / confirm ignored

Most browsers block dialogs in unload. For leave prompts, use beforeunload carefully.

bfcache

Unload can break caching

Unload listeners often prevent the back/forward cache. Prefer pagehide for teardown.

Browser Support

.unload() existed from jQuery 1.0, was deprecated in 1.8, and removed in 3.0. In jQuery 3.x use .on("unload") / .trigger("unload"). The underlying unload event still varies by browser.

✓ Removed · jQuery 3.0+

jQuery .unload()

Unavailable in current jQuery. Legacy labs use jQuery 2.2.4. Prefer .on('unload') only when you must, and pagehide for new cleanup logic.

0% In jQuery 3.x
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 API

Bottom line: Do not ship .unload() on jQuery 3+. Migrate to .on('unload') / .trigger('unload'), or better: pagehide for session-end cleanup.

Performance Notes

ApproachCostBest for
Light cleanup / storage writeCheapLegacy unload / pagehide
pagehide cleanupCheapModern session-end logic
Heavy sync work on unloadExpensive / droppedAvoid — browsers may kill it

Keep leave-time work tiny. Prefer navigator.sendBeacon or async patterns started earlier — do not rely on long unload handlers.

Key Takeaways

  • Role: legacy bind/trigger for the window unload event.
  • Migrate: $(window).on("unload", fn) — required on jQuery 3+.
  • Trigger: use .trigger("unload") instead of no-arg .unload().
  • Prefer: pagehide for modern page-lifecycle cleanup.

One line: .unload() is removed — use .on("unload") to migrate, and pagehide for new cleanup.

Frequently Asked Questions

.unload(handler) bound a function to the browser unload event — typically on $(window). .unload() with no arguments triggered unload handlers. It was shorthand for .bind("unload", handler) or .trigger("unload"). Removed in jQuery 3.0.
No. .unload() was deprecated in jQuery 1.8 and removed in jQuery 3.0. Calling it in jQuery 3.x throws because $.fn.unload is undefined. Use $(window).on("unload", handler) and $(window).trigger("unload") 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 for more reliable page-lifecycle code.
They target the same browser unload event. .unload() was sugar removed in 3.0. .on("unload", fn) is the supported API in jQuery 3.x and supports eventData, namespaces, and .off() cleanup.
Generally no. Browsers treat unload as unreliable — it can break the back/forward cache and may not fire on mobile tab switches. Prefer pagehide or visibilitychange for cleanup; use beforeunload only for unsaved-changes warnings.
jQuery 3.0 removed event shorthands that shared names with other APIs or had limited value. Prefer the explicit .on() / .trigger() forms for unload.

Next: click event

Continue with everyday mouse interaction using the click event.

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.

8 people found this page helpful