Description#
Implement the standard page lifecycle events in their correct firing order. These events are critical for web applications that need to initialize on load and clean up on unload.
Requirements#
Event Ordering (page load)#
- HTML parsing begins
- Synchronous scripts execute as encountered
- HTML parsing completes
DOMContentLoadedfires ondocument(bubbles towindow)- Deferred scripts execute
- Images, stylesheets, and other subresources finish loading
loadevent fires onwindow
Event Ordering (page unload / navigation away)#
beforeunloadfires onwindow— handler can callevent.preventDefault()to show a confirmation dialogpagehidefires onwindowunloadfires onwindow- Document is discarded and new navigation proceeds
DOMContentLoaded#
- Fires when the HTML document has been completely parsed
- Does not wait for stylesheets, images, or subframes
- Event type:
Event(notCustomEvent) - Registered via
document.addEventListener('DOMContentLoaded', ...)
load#
- Fires when the page and ALL dependent resources (images, stylesheets, scripts) have loaded
- Fires on
window, also on individual elements (<img>,<script>,<link>) window.onloadand<body onload>are equivalent
beforeunload#
- Allows page to prompt user before leaving
- If
event.preventDefault()is called orevent.returnValueis set, show confirmation - Confirmation dialog text is browser-controlled (ignore custom message)
unload#
- Fires when the document is being unloaded
- Last chance for cleanup; no new navigations allowed during handler
Acceptance Criteria#
-
DOMContentLoadedfires after HTML parsing completes -
loadfires after all resources are loaded -
beforeunloadfires before navigating away and can cancel navigation -
unloadfires during document teardown - Events fire in the correct order
-
document.readyStatetransitions:loading→interactive→complete - Unit tests for event ordering and readyState transitions