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.
Syntax
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).
Values
Attribute Values
The attribute value is a script. Common placements:
Value / target
Description
Try it
<body onload="…">
Page + dependent resources finished (alias of window.onload)
Listen on window. Matches MDN’s preferred page-level pattern.
Does not replace others. Unlike assigning window.onload = …, addEventListener can stack handlers.
Still waits for resources. Same timing as <body onload>.
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet ExplorerLegacy support
Supported
onloadWidely supported
Bottom line: Safe to use. Prefer addEventListener('load') in apps; inline onload is fine for learning and early registration on resources.
Remember
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.