Languages
[Edit]
EN

JavaScript - load events order for window, document, dom, body and elements

5 points
Created by:
Root-ssh
178170

In this quick article, we are going to discuss about order of load events.

Kowledge about order helps to execute some logic when DOM is ready, before resources are loaded or after all resources (scripts, styles, images, videos, audio, frames) are loaded. readystatechange,with readyState="complete", body onload and window onload are designed to work with synchronous loading - it means if some file is loaded asynchronously it will be not signaled in event.

 

By default load/change events should be executed in following order:

 

Element

(or Object)

Event nameOccurs when:
1Document

readystatechange

with readyState === "interactive"

DOM is parsed but resources are not loaded yet.
2DocumentDOMContentLoadedDOM is parsed but resources are not loaded yet.
3WindowDOMContentLoadedDOM is parsed but resources are not loaded yet.
4Document

readystatechange

with readyState === "complete"

DOM is parsed and resources are loaded.
5ImageonloadImage resource is loaded.
6FrameonloadFrame resource is loaded.
7Bodyonload

DOM is parsed and resources are loaded.

(can be handled after window onload - read example notes)

8Windowonload

DOM is parsed and resources are loaded.

(can be handled before body onload - read example notes)

 

Events comparision example

Edit

Note: load events for window and body can be executed in different order - depending of in what order they were added.

Used resources:

Image used in onload event order test - JavaScript
Image used in onload event order test - JavaScript

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join