The onload
property of the GlobalEventHandlers
mixin is an event handler for the load
event of a Window
, XMLHttpRequest
, <img>
element, etc., which fires when the resource has loaded.
window.onload = funcRef;
funcRef
is the handler function to be called when the window’s load
event fires.
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>
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 Gecko-Specific 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.
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'onload' in that specification. | Living Standard | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
DOMContentLoaded
event in Listening to events: Simple DOM events
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload