English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

HTML tag list

HTML onload event attribute

The load event is triggered when a resource and its dependent resources have completed loading.

HTML Event Attributes

Online Example

Execute JavaScript immediately after page load:

<!DOCTYPE html>
<html>
<head>
<title>HTML onload event attribute usage (Basic Tutorial Website oldtoolbag.com)</title>
<script>
function myFunction() {
  alert("Page is loaded");
}
</script>
</head>
<body onload="myFunction()">
<h1>Hello World!</h1>
</body>
</html>
Test and see ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the onload event attribute.

Definition and Usage

The onload attribute is triggered after the loading of an object.
Once the webpage has completely loaded all content (including images, script files, CSS files, etc.), onload is typically used within the <body> element to execute scripts. However, it can also be used on other elements (see the 'Supported HTML Tags' below).
The onload attribute can be used to check the visitor's browser type and version, and load the correct version of the webpage based on this information.
The onload attribute can also be used to handle cookies.

HTML 4.01 and HTML5The differences between

No difference.

Syntax

<element onload="script">

Attribute Value

ValueDescription
scriptSpecifies the script to be executed when the onload event is triggered.
HTML Event Attributes