HTML onload Attribute

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onload attribute is an inline event handler for the load event. On <body> (an alias for window.onload) it runs when the whole page and its dependent resources finish loading — stylesheets, scripts, images, iframes, and so on (except lazily loaded resources).

On resource elements such as <img>, <iframe>, or <script>, it runs when that resource loads successfully. Pair failures with onerror.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

Prefer window in script.

MDN recommends attaching page-level load handlers to window rather than relying on <body onload>, for clearer intent.

HTML Syntax

syntax.html
<element onload=" script">

script is JavaScript that runs when loading succeeds. Script alternative: window.addEventListener("load", handler) or element.addEventListener("load", handler).

Attribute Values

The attribute value is a script. Common placements:

Value / targetDescriptionTry it
<body onload="…">Page + dependent resources finished (alias of window.onload)Try it
<img onload="…">Image resource loaded successfullyTry it
handler() / handler(event)Call a named function; optionally pass the event objectTry it
addEventListener("load")Modern script registration (preferred in apps)Try it

Live Preview

This tutorial page has already fired window load. Reload the demo image to see an element-level onload run again.

action

Click Reload image to fire img onload again.

Demo
Demo diagram for onload

Waiting for image load…

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Run Code When the Page Loads

Classic beginner pattern: initialize UI after the full page load.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>onload body</title>
</head>
<body onload="pageReady()">
  <p id="status">Loading…</p>

  <script>
    function pageReady() {
      document.getElementById("status").textContent =
        "Page and resources finished loading.";
    }
  </script>
</body>
</html>
Try It Yourself

How It Works

  1. Put onload on <body>. It mirrors window.onload.
  2. Wait for resources. Images and stylesheets finish before the handler runs.
  3. Then update the DOM. Safe to measure layout or touch late-loading assets.
  4. For DOM-only setup, a script at the end of <body> (or DOMContentLoaded) is often enough — no need to wait for every image.

Example 2 — React When an Image Loads

Use onload on <img> to confirm a successful image fetch.

HTML
<img
  id="photo"
  src="/images/area-example.png"
  alt="Diagram"
  width="200"
  height="200"
  onload="imageReady()">
<p id="msg">Waiting for image…</p>

<script>
  function imageReady() {
    document.getElementById("msg").textContent = "Image loaded.";
  }
</script>
Try It Yourself

How It Works

  1. Browser fetches src. See also src.
  2. On success, load fires. Your onload handler runs.
  3. HTML attribute timing. Inline onload is registered early, so it is less likely to miss a cached image than a late script listener.
  4. On failure, use onerror — onload does not run.

Example 3 — Modern window.addEventListener

Same page-load idea without an inline attribute — clearer in larger projects.

HTML
<p id="status">Waiting for load…</p>

<script>
  window.addEventListener("load", function () {
    document.getElementById("status").textContent =
      "window load fired — resources ready.";
  });
</script>
Try It Yourself

How It Works

  1. Listen on window. Matches MDN’s preferred page-level pattern.
  2. Does not replace others. Unlike assigning window.onload = …, addEventListener can stack handlers.
  3. Still waits for resources. Same timing as <body onload>.
  4. Scripts with defer / async still participate in the page load timeline — load waits for them as documented.

Edge Cases & Pitfalls

Most bugs are timing races or using page load when you only needed the DOM.

timing

Resource may load before your listener

Cached images can finish before a late script runs. Use an HTML onload, set src after addEventListener, or check img.complete.

scope

load does not bubble to window

An img load does not bubble up as a window load. Listen on the element (or use body/window only for the document load).

perf

Do not wait for load for simple DOM work

Place scripts before </body> or use DOMContentLoaded when you do not need images/stylesheets first.

lazy

Lazy images may skip the window load wait

MDN: lazily loaded resources are excluded from delaying the page load event. See loading.

assign

window.onload = replaces earlier handlers

Assignment overwrites. Prefer addEventListener("load", …) when multiple scripts may listen.

Browser Support

onload is an HTML event handler for the load event in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onload

Widely available since about July 2015. Supported on window/body and resource elements such as img, iframe, script, and link.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy support
Supported
onload Widely supported

Bottom line: Safe to use. Prefer addEventListener('load') in apps; inline onload is fine for learning and early registration on resources.

Key Takeaways

  • Role: run script when the page or a resource finishes loading successfully.
  • body ≈ window: page-level load; element onload is per resource.
  • vs DOMContentLoaded: load waits for dependent resources; DOMContentLoaded does not.
  • Errors: failed loads fire onerror, not onload.

One line: use onload when you need resources ready — not only the HTML tree.

Frequently Asked Questions

It is an inline event handler for the load event. On body (or window) it runs when the page and its dependent resources have finished loading. On elements like img or iframe it runs when that resource loads successfully.
Yes in practice. MDN notes that load on the body element is an alias for window.onload. Prefer attaching the handler to window in script for clarity.
DOMContentLoaded fires when the HTML document is parsed — it does not wait for images and stylesheets. The window load event waits for those dependent resources (except lazy-loaded ones).
Common ones: body, img, iframe, script, link, embed, object, and style. On body it means the whole page load; on others it means that element’s resource finished loading.
The image may have finished loading before your script added the listener. Use an HTML onload attribute, set src after addEventListener, or check image.complete after registering.
No. The load event is Baseline widely available. Inline handlers are discouraged for large apps in favor of addEventListener, but onload itself is not deprecated.

Next: onloadeddata

Media event when the current frame of data is available.

onloadeddata tutorial →

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