domReady
The domReady
is an alias for the DOMContentLoaded
event. Once the initial HTML
document has been fully loaded and parsed, the DOMContentLoaded
event will be triggered without waiting for the complete loading of stylesheets, images, and subframes.
The browser's rendering of the DOM
structure follows a specific sequence. Although different browsers may have different implementations, the basic process is generally the same:
DOM Tree
.<link>
or <style>
tags, generating the CSSOM
. It's important to note that the parsing of HTML tags and CSS is done in parallel at this stage.<script>
tag, the browser immediately starts parsing the script and pauses document parsing. This is because scripts can potentially modify the DOM
and CSS
, and continuing parsing would be a waste of resources. Therefore, <script>
tags should be placed after the <body></body>
.DOM Tree
and CSSOM
are generated, the two are combined for layout, computing their size, position, and other layout information, creating an internal representation model that can capture all this information - known as the render tree
.paint
method to display content on the screen.There are blocking processes during the browser's parsing of the DOM
structure:
DOM
tree. These two parsing processes can be done in parallel. However, during the CSS loading process, JavaScript parsing cannot be performed. Additionally, since the Render Tree
generation requires CSSOM
, the Render Tree
is not generated when the DOM Tree
parsing is complete but the CSSOM
is not.DOM Tree
parsing is incomplete while the CSSOM
is complete, the Render Tree
is also not generated.<script>
tags does not block DOM
parsing and does not block the triggering of the DOMContentLoaded
event, but it still blocks the triggering of the load
event.Now, let's take a look at the timing of the triggering of the DOMContentLoaded
event and the load
event:
HTML
document has been fully loaded and parsed, the DOMContentLoaded
event is triggered without the need to wait for stylesheets, images, and subframes to be fully loaded. Regarding the timing of the trigger, if the document consists only of HTML
and CSS
, the DomContentLoaded
event can be triggered without waiting for the complete loading of CSS
. When the Js
comes before CSS
, the DomContentLoaded
event can be triggered without waiting for the completion of CSS
loading, but the parsing of CSS
and DOM
needs to wait for the preceding Js
parsing to be completed. When Js
comes after CSS
, the DomContentLoaded
event needs to wait for the completion of CSS
and Js
loading. As mentioned earlier, the loading of CSS
blocks the loading of Js
, and since the Js
tag itself is part of the DOM
structure, the DomContentLoaded
event can only be triggered after its loading is complete. Asynchronous loading of <script>
tags does not block the DOMContentLoaded
event.load
event is triggered when the entire page and all dependent resources such as stylesheets and images have finished loading. The use of non-dynamically loaded <iframe>
also blocks the load
event, and even asynchronous loading of <script>
tags blocks the load
event.Rearranging the process of page loading under various conditions primarily revolves around the timeline of the DOMContentLoaded
and load
events:
document.readyState = "loading"
.<link>
or <style>
tags, the CSS is parsed to generate the CSSOM. It is noteworthy that the parsing of HTML tags and CSS is executed in parallel at this point.<script>
without asynchronous loading, the document parsing is blocked, waiting for the JavaScript to load and execute before continuing to parse the document.<script>
, the document parsing continues without blocking, and the defer
attribute will make the JS file wait to execute until the DOM Tree is constructed, while the async
attribute will make the JS file execute immediately after downloading.src
, and the image resource is asynchronously loaded without blocking the document parsing. However, note that the browser imposes a limit on the maximum number of threads that can be opened for a domain.document.readyState = "interactive"
.defer
attribute start to execute in order.DOMContentLoaded
event is triggered.async
attribute, as well as images, <iframe>
, and so on, until the page is completely loaded.load
event is triggered, and document.readyState = "complete"
.Sometimes, we want to intervene in the DOM
as soon as possible, and in this case, calling the DOMContentLoaded
event is obviously more appropriate. However, to handle different browsers, it needs to be made compatible.
DOMContentLoaded
event for browsers that support it.Webkit
versions below 525
, use polling of document.readyState
to implement it.IE
browsers, use the well-known hack
discovered by Diego Perini
.