The load event fires on an element when it and all of its sub-resources have finished loading. It applies to elements with a URL — <img>, <script>, <iframe> — and to window. Bind with .on("load", handler) (since 1.7) and fire it for tests with .trigger("load"). This is the browser event, not the Ajax method .load(url).
Syntax
Start with these common calls. Keep them short and readable.
jQuery
// Image: bind BEFORE setting src
$("#book")
.on("load", function () {
// image finished loading
})
.attr("src", "photo.png");
// Whole page: DOM + images, stylesheets, frames
$(window).on("load", function () {
// every asset is ready
});
// iframe
$("#frame").on("load", function () { /* frame content loaded */ });
// Fire for tests
$("#book").trigger("load");
// Not this - that is the Ajax method:
// $("#box").load("/partial.html");
Call
What it does
.on("load", handler)
Run handler when the element has loaded
.on("load", eventData, handler)
Same, with data available as event.data
.one("load", handler)
Run the handler at most once
$(window).on("load", handler)
Wait for the full page and every asset
.trigger("load")
Fire bound handlers without a real load
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the element: an <img>, <iframe>, <script>, or window.
Step 2
Attach .on("load", handler)before setting src.
Step 3
Set src (or let the page load). The handler receives an event object; this is the element.
Return Value
Return Value
Description
jQuery object
.on() returns the original collection so you can chain .attr("src", ...) and more.
jQuery Examples
Example 1 — Image: Bind Before Setting src
Attach load first, then set src so a fast or cached load is not missed.
jQuery
$("#book")
.on("load", function () {
$("#log").text("Image loaded: " + this.naturalWidth + "x" + this.naturalHeight);
})
.attr("src", "photo.png");
Load.$(window).on("load") runs after images, stylesheets and frames finish.
Choose. Use ready for DOM work; use load when you need image sizes or full layout.
Example 3 — iframe load and .trigger("load")
Know when a frame has loaded, and fire the handler manually for tests.
jQuery
$("#frame")
.on("load", function () {
$("#log").text("iframe loaded");
})
.attr("srcdoc", "<p>Hello from the frame</p>");
$("#fire").on("click", function () {
$("#frame").trigger("load"); // test without reloading
});
Bind. The handler is attached before the frame content is set.
Load. Setting srcdoc (or src) loads the frame and fires load.
Trigger..trigger("load") runs handlers without a network request.
Frequently Asked Questions
The load event is sent to an element when it and all its sub-elements have finished loading. It works for elements associated with a URL, such as images, scripts, frames and iframes, and for the window object. Bind it with .on("load", handler).
They share a name but are unrelated. .on("load", handler) listens for a resource finishing. .load(url) is an Ajax method that fetches HTML from a URL and inserts it into the matched elements. See the Ajax .load() tutorial for the second one.
The browser can finish loading as soon as src is set, especially for cached images. If the handler is not attached yet, you miss the event. Chain .on("load", fn) first, then .attr("src", url).
Document ready fires when the DOM has been parsed. The window load event fires later, after all images, stylesheets and frames have loaded. Use ready for DOM manipulation and window load when you need asset dimensions.
No. The load event does not bubble, so a delegated handler on a parent will not catch it from a child image. Attach the handler directly to each element, or to window for the page.
Common causes are binding after src was set, a cached image that loaded before the handler existed, or a broken URL (which fires error instead). Bind first, and check the img.complete property for cached images.
No. The .load(handler) event shorthand was deprecated in jQuery 1.8 and removed in 3.0. Use .on("load", handler) instead.