大网数据广告活动
Web 标准与前端开发

HTMLElement: load event

· 52CE 我爱测

The **`load`** event fires for elements containing a resource when the resource has successfully loaded. Currently, the list of supported HTML elements are: embed, iframe, img, link, object, script, style, and track.

HTML DOM

The load event fires for elements containing a resource when the resource has successfully loaded. Currently, the list of supported HTML elements are: embed, iframe, img, link, object, script, style, and track.

[!NOTE] The load event on HTMLBodyElement#event_handlers is actually an alias for the Window/load_event event. Therefore, the load event will only fire on the <body> element once all of the document's resources have loaded or errored. However, for the sake of clarity, it is recommended that the event handler is attached to the window object directly rather than on HTMLBodyElement.

This event is not cancelable and does not bubble.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("load", (event) => { })

Alternatively, assign to the event handler property of the interface:

onload = (event) => { }

Event type

A generic Event.

Usage notes

Handling resources that have already loaded

A resource may finish loading before your script registers a load event listener. In that case, the listener will not receive the event that has already fired.

For example, an image in the HTML may load while the browser is still receiving and parsing the rest of the document, before it reaches a subsequent script that registers the listener. The listener can also be registered too late if the script is loaded asynchronously or deferred, or if it waits for Document/DOMContentLoaded_event before registering the listener. Server-rendered HTML using frameworks such as React or Vue can have the same issue because event handlers written in JSX or templates are compiled into JavaScript calls, not HTML event handler attributes.

There are some ways to ensure that the event handler is registered as soon as possible, before the resource loads. For example, you can use an HTML event handler attribute if you don't mind its undesirable aspects, or you can dynamically create the whole element in JavaScript and make sure the event listener is attached before starting the load (such as by assigning to src for images).

Alternatively, when registering the event handler, you can check if the resource has already loaded—and if so, immediately trigger the handler. For an image, you can check its HTMLImageElement.complete and HTMLImageElement.naturalWidth properties after registering the listener. complete checks that the request completed; naturalWidth > 0 ensures that an actual image was loaded.

const image = document.getElementById("image");
let handled = false;

function handleLoaded() {
  if (handled) return;
  handled = true;
  // Use the loaded image here.
}

image.addEventListener("load", handleLoaded, { once: true });

if (image.complete && image.naturalWidth > 0) {
  handleLoaded();
}

Examples

This example prints to the screen whenever the img element successfully loads its resource.

HTML

<img
  id="image"
  src="/shared-assets/images/examples/favicon144.png"
  alt="MDN logo"
  width="72" />
<div><button>Reload</button></div>

JavaScript

const image = document.getElementById("image");
image.onload = () => {
  document.body.appendChild(document.createElement("div")).textContent =
    "loaded!";
};

document.querySelector("button").addEventListener("click", reload);

function reload() {
  image.src = "/shared-assets/images/examples/favicon144.png";
}

Result

The example's <img> element contains an src attribute in the markup, so the image may load before the load event listener attaches. Clicking "reload" is guaranteed to trigger the event listener.

examples

Specifications

Browser compatibility

See also

  • Related events
    • Window: Window/load_event event
    • Window: Window/error_event event

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。