jQuery .unload() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Event Handler

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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);
CallWhat it does
windowStandard target for page unload
handlerfunction(event)
eventDataExtra data as event.data
jQuery versionMethod missing in 3.x

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1In old code, bind with $(window).unload(handler) (jQuery 1.x / 2.x only).
Step 2Optionally pass eventData: .unload(data, handler) → .on("unload", data, handler).
Step 3Trigger with no-arg .unload() (legacy) or .trigger("unload") (preferred).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: Mouse Events

Continue with everyday mouse interaction using the click event.

Mouse Events tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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