YouTip LogoYouTip

Event Load

## jQuery load() Event Method The jQuery `load()` event method attaches an event handler to the `load` event of a specified element. The `load` event fires when the element and all its sub-resources (such as images, scripts, frames, and iframes) have fully loaded. It can also be applied to the `window` object to detect when the entire page has finished rendering. --- ### Important Deprecation Notice > ⚠️ **Deprecated:** The `.load()` event shortcut method was **deprecated in jQuery version 1.8** and **removed in jQuery 3.0**. > > To bind a load event in modern jQuery applications, you should use the `.on()` method instead: > ```javascript > $(selector).on("load", handler); > ``` > **Note on Ambiguity:** jQuery also has an AJAX method named `.load()`. The library distinguishes between the two based on the arguments passed to the method. --- ## Syntax ### Deprecated Syntax (jQuery < 3.0) ```javascript $(selector).load(function) ``` ### Modern Recommended Syntax (jQuery 1.8+) ```javascript $(selector).on("load", function) ``` ### Parameter Values | Parameter | Type | Description | | :--- | :--- | :--- | | `function` | Function | **Required.** Specifies the function to run when the selected element has fully loaded. | --- ## Code Examples ### Example 1: Triggering an Alert When an Image Loads This example displays an alert box as soon as an image is fully loaded onto the page. ```javascript // Deprecated syntax $("img").load(function() { alert("The image has successfully loaded."); }); // Modern recommended syntax $("img").on("load", function() { alert("The image has successfully loaded."); }); ``` ### Example 2: Changing Text After an Image Loads This example updates the text inside a `
` element once a specific image has finished loading. ```html Product
Loading image...
``` ### Example 3: Detecting When the Entire Page Has Loaded You can bind the `load` event to the `window` object to execute code only after the entire page (including DOM, CSS, images, and iframes) is fully loaded. ```javascript $(window).on("load", function() { console.log("The entire page, including all resources, is fully loaded."); }); ``` --- ## Technical Considerations & Best Practices 1. **Browser Caching Issues:** Depending on the browser (especially older versions of Firefox and Internet Explorer), the `load` event may not fire if the image is already cached in the user's browser. 2. **Event Bubbling:** The `load` event does not bubble up the DOM tree. If you want to listen to load events on child elements, you must bind the event handler directly to the target elements (like `` tags) rather than relying on delegation. 3. **Alternative for DOM Readiness:** If you only need to wait for the HTML DOM structure to be ready (without waiting for heavy images or stylesheets to load), use `$(document).ready()` instead of `$(window).on("load")`.
← Event MousedownEvent Live β†’