HTML Document Lifecycle and Readiness
Observe parser and readiness states
The document starts in loading while parsing. interactive means parsing finished; complete is reached when the document has completed the relevant load work. readyState is a snapshot, not an event history. A parser-inserted classic inline script in this full document observes loading.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Readiness</title> </head> <body> <output id="result"></output> <script> document.querySelector("#result").textContent = document.readyState; </script> </body></html>The inline script writes loading.
Compare DOMContentLoaded and load
DOMContentLoaded fires after parsing and execution of deferred classic scripts and non-async parser-inserted module scripts as applicable. It does not generally wait for images or async scripts. The window load event occurs later after relevant resources finish loading. A small page may pass through states very quickly.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Events</title> </head> <body> <output id="result"></output> <script> const log = []; function record(event) { log.push(event + ":" + document.readyState); document.querySelector("#result").textContent = log.join(" | "); } document.addEventListener("DOMContentLoaded", () => record("DOM")); window.addEventListener("load", () => record("load")); </script> </body></html>Expected: DOM:interactive | load:complete
Initialize safely even after readiness
An async or dynamically inserted script may execute after DOMContentLoaded. Check readyState before adding a listener: if parsing is still underway, wait; otherwise run the initialization. This pattern ensures DOM availability, not that every other module or resource has completed. In this demonstration the function is called from load to exercise the late branch.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Init</title> </head> <body> <output id="result"></output> <script> function init() { document.querySelector("#result").textContent = "Ready"; } function start() { if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", init, { once: true }); } else { init(); } } window.addEventListener("load", start, { once: true }); </script> </body></html>Expected: Ready
Common mistakes and practice
Use DOMContentLoaded for DOM-dependent initialization if the listener is registered early, or the readiness guard for reusable scripts. Use load only when its resource-loading semantics are needed. Avoid arbitrary timers as a replacement for lifecycle signals.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Document Lifecycle and Readiness — Knowledge Check
Apply the internals concepts taught in this lesson.
Which readiness state represents active parsing?
Which event follows parsing and applicable deferred script execution?
Does DOMContentLoaded generally wait for all images?
Which event is associated with later resource load completion?
Why check readyState before listening for DOMContentLoaded?