onload 特性为 GlobalEventHandlers 混合 EventHandler 处理 load 事件在 Window , XMLHttpRequest , <img> 元素、等。

load 事件激发,当给定资源已加载时。

句法

target.onload = functionRef;
					

functionRef 是要被调用的处理程序函数当窗口的 load 事件激发。

范例

window.onload = function() {
  init();
  doSomethingElse();
};
					
<!doctype html>
<html>
  <head>
    <title>onload test</title>
    // ES5
    <script>
      function load() {
        console.log("load event detected!");
      }
      window.onload = load;
    </script>
    // ES2015
    <script>
      const load = () => {
        console.log("load event detected!");
      }
      window.onload = load;
    </script>
  </head>
  <body>
    <p>The load event fires when the document has finished loading!</p>
  </body>
</html>
					

注意事项

load 事件在文档加载过程结束时激发。此时,文档中的所有对象都在 DOM 中,且所有图像、脚本、链接和子框架均已完成加载。

还有 DOM Events like DOMContentLoaded and DOMFrameContentLoaded (可以被处理使用 EventTarget.addEventListener() ) 在页面 DOM 构造之后被激发,但不等待其它资源完成加载。

规范

规范 状态 注释
HTML 实时标准
在该规范中的 onload 定义。
实时标准 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onload Chrome 1 Edge 12 Firefox 1 IE 9 Opera 9 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: