The modern replacement of the (on)"DOM ready" is the DOMContentLoaded event.
You can listen on it on 2 global objects:
window=> https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_eventdocument=> https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event
Now I know that the original target of this event is document however according to the MDN link above there's also ability to "listen for this event on the Window interface to handle it in the capture or bubbling phases.".
Is there a particular advantage or a special case when it would be more appropriate or even required to catch the event on the window - for either the capture or the bubble up phase?
I came back to this after some time and it started to make sense:
the
Windowlistener happens AFTER theDocumentevent - because it bubbles FROM the Documentso if you run (jsfiddle):
...the output in the alert will be:
Which proves it's from Document since
window.constructor.nameis"Window"This is even described in the Living Standard which reads (as of 2023-06-20)
And also in the
Eventspart of the docs:There is no mention of
DOMContentLoadedforwindowspecifically :-)So to answer the main question
The difference is when you want your handler to be executed.
AFTER ALL
documentDOMContentLoadedlisteners have been executed=> use
window.addEventListener("DOMContentLoaded", ...Among all other handlers listening to
DOMContentLoadedonDocument=> use
document.addEventListener("DOMContentLoaded", ...Sidenote: MDN mentions
loadevent on the "Document: DOMContentLoaded event" page - that is taken out of context and what they mean is to useloadevent listener onWindow-does not have aDocumentloadevent since it isn't anElement...