Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onload — handling the load event

The onload attribute contains JavaScript that handles the load event. On the body element, it concerns page loading and handles the Window object's event rather than an event on body itself.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onload="JavaScript statements"

onload is an event handler content attribute for load. Its value is a JavaScript function body, not a script file address. Adding the attribute does not itself start loading a resource.

On the body element, onload handles the Window object's event. It therefore does not mean only that the body element is ready. The page event occurs after parsing has finished and conditions delaying page loading have cleared; DOMContentLoaded occurs earlier in this process and is not interchangeable with it.

Attribute code is executable JavaScript: do not insert untrusted data into it. Content Security Policy can block these inline handlers.

The example changes the paragraph text from Waiting to Loaded after the page loads. It fetches no external resources. Save it as an HTML file and open it in a browser with JavaScript enabled and no policy blocking inline handlers.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>onload example</title>
</head>
<body onload="document.getElementById('status').textContent = 'Loaded'">
  <p id="status">Waiting</p>
</body>
</html>

Result

Loaded

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources