👀 Live Preview
This tutorial page already fired load — status below was set when resources finished:
Checking load state…

The onload attribute is an inline event handler that runs JavaScript when the load event fires — when a resource has finished loading. On <body onload="…"> or window.onload, that means the page and its subresources (images, stylesheets, etc.) are loaded. On <img>, <iframe>, or <script>, it fires when that specific element finishes loading. Use it to initialize UI, hide loading spinners, or read image dimensions. For “DOM is ready” (before all images), prefer DOMContentLoaded. Modern pages often use addEventListener("load", …) or defer on scripts instead of inline body onload.
Inline JS.
Fully loaded.
Page ready.
Image ready.
Preferred way.
Timing.
onload AttributeThe primary purpose of onload is to run code after loading completes. On a full page, that is when the browser has fetched the document and its dependent resources — so measurements that need rendered images or layout are safe. On an individual <img>, you can swap a placeholder for the real picture or show width and height once the file arrives.
Many apps only need the HTML parsed, not every image — in that case DOMContentLoaded fires earlier and feels snappier. Choose load when you truly depend on all subresources being ready.
In HTML documents, <body onload> sets the window’s load handler. window.addEventListener("load", fn) is the modern equivalent.
Set onload on body, img, iframe, or assign window.onload:
<body onload="onPageLoad()">
<!-- page content -->
</body>
<img src="photo.jpg" alt="Photo" onload="imageReady(this)">load event fires on that element.body onload / window.onload — entire page and subresources loaded.img onload — that image file loaded (also fires for cached images).iframe onload — embedded document loaded.window.addEventListener("load", handler).onerror for failures.load event does not bubble from window in the same way as clicks — attach to the target you care about.The onload attribute accepts a string of JavaScript code or function reference:
onload="onPageLoad()" — Call a named function when the page loads.onload="imageReady(this)" — Pass the loaded element.window.onload = function() { … }.window.addEventListener("load", () => {
document.getElementById("loader").hidden = true;
initDashboard();
});
document.addEventListener("DOMContentLoaded", () => {
// DOM ready — images may still be loading
wireUpButtons();
});| Target | load fires when | Common use |
|---|---|---|
body / window | Page + subresources loaded | Hide loader, full init |
img | Image file loaded | Reveal image, read size |
iframe | Embedded doc loaded | Resize iframe, postMessage |
| HTML parsed only | No (use DOMContentLoaded) | Faster DOM setup |
| Resource failed | No | Use onerror |
| Handler attribute | onload | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<body> | Yes | Classic page load hook |
window (via script) | Yes | Recommended registration |
<img> | Yes | Per-image load |
<iframe> | Yes | Embedded page load |
<script>, <link rel="stylesheet"> | Yes | Resource load (rare inline use) |
<input> | No | Not a loadable resource element |
load vs DOMContentLoaded vs defer| Approach | When code runs | Typical use |
|---|---|---|
onload / load | All page resources loaded | Needs images/layout ready |
DOMContentLoaded | HTML parsed | Wire up DOM early |
<script defer> | After parse, before load | Modern script loading |
Page init on body load, addEventListener on window, and image onload feedback.
This tutorial page already fired load — status below was set when resources finished:
Checking load state…
Run setup when the full page has loaded:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Onload Example</title>
</head>
<body onload="onPageLoad()">
<p id="status">Loading…</p>
<script>
function onPageLoad() {
document.getElementById("status").textContent =
"Page loaded successfully!";
}
</script>
</body>
</html>The browser fires load on the window after the document and its resources finish loading. The inline body onload handler runs your init function once.
Attach the page load handler with addEventListener:
<script>
window.addEventListener("load", function () {
console.log("Page load handler executed!");
document.getElementById("app").classList.remove("loading");
});
// Legacy property form:
document.body.onload = function () { /* … */ };
</script>Register once in a script. The callback runs a single time when the page load completes.
Show feedback when an image finishes loading:
<img src="/images/codetofun-logo.png" alt="Logo"
onload="imageReady(this)">
<p id="imgStatus"></p>
<script>
function imageReady(img) {
document.getElementById("imgStatus").textContent =
"Loaded " + img.naturalWidth + "×" + img.naturalHeight + " px";
}
</script>Each image fires its own load event. Use this or the parameter to read naturalWidth and naturalHeight after decode.
aria-busy="false" when loading finishes on dynamic regions.onload does not replace accessible alt attributes.HTML, CSS, images.
Parse + download.
onload handler runs.
UI, charts, images ready.
The load event and onload handler are supported in all modern browsers on window, body, img, and iframe.
onload supportAll major browsers fire load on the window and on loadable elements.
Bottom line: Reliable everywhere; prefer addEventListener("load") and DOMContentLoaded when appropriate.
DOMContentLoaded when you only need the DOM treeload when images or iframes must be ready firstwindow.addEventListener("load", …) over inline body onloadimg onload with onerror for robust media handlingrequestIdleCallback if neededalert() on every page loadload when DOMContentLoaded is enoughonload stringsload fires again on SPA route changes — it does notThe onload attribute runs JavaScript when loading finishes — on the full page via body / window, or on individual resources like images and iframes.
Prefer addEventListener("load", …), use DOMContentLoaded when you do not need every asset, and show user feedback without blocking alerts.
onloadBookmark these before wiring page and resource load handlers.
Page + assets.
EventPage init.
ScopeEarlier hook.
ComparePer resource.
PatternModern way.
APIload event fires — when a page or element has finished loading its resource(s).window.addEventListener("load", handler). Inline body onload works for learning and simple pages.DOMContentLoaded = HTML parsed. load = full page including images and other subresources.img onload runs when that image file has loaded successfully.onerror for failed resource loads.Practice the onload attribute with body init and image load examples in the Try It editor.
5 people found this page helpful