The onload property of the GlobalEventHandlers mixin is an EventHandler that processes load events on a Window, XMLHttpRequest, <img> element, etc.
The load event fires when a given resource has loaded.
Syntax
target.onload = functionRef;
Value
functionRef is the handler function to be called when the windowβs load event fires.
Examples
window.onload = function() {
init();
doSomethingElse();
};
<!doctype html>
<html>
<head>
<title>onload test</title>
// ES5
<script>
function load() {
console.log("load event detected!");
}
window.onload = load;
</script>
// ES2015
<script>
const load = () => {
console.log("load event detected!");
}
window.onload = load;
</script>
</head>
<body>
<p>The load event fires when the document has finished loading!</p>
</body>
</html>
Notes
The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
There are also DOM Events like DOMContentLoaded and DOMFrameContentLoaded (which can be handled using EventTarget.addEventListener()) which are fired after the DOM for the page has been constructed, but do not wait for other resources to finish loading.
Specifications
| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'onload' in that specification. |
Living Standard | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
onload | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 9 | Opera Full support 9 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 |
Legend
- Full support
- Full support
See also
loadeventDOMContentLoadedevent in Listening to events: Simple DOM events- IIFE Immediately-invoked function expression
