admin管理员组文章数量:1296910
The modern replacement of the (on)"DOM ready" is the DOMContentLoaded
event.
You can listen on it on 2 global objects:
window
=>document
=>
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?
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/en-US/docs/Web/API/Window/DOMContentLoaded_eventdocument
=> https://developer.mozilla/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?
- 1 It seems more like a side-effect of event bubbling than an actual feature. – Konrad Commented Aug 20, 2022 at 10:26
-
1
@KonradLinkowski in this moment I can only imagine 1 case where you'd want to do that and that is if you need something to be run before some
document
bind, but you can't put your code before - but - it does not seem to work this way and thedocument
event is always called first, even thoughwindow
was bound first - jsfiddle/yzhm8puL so maybe it could be used the other way around - if you want to run something after ALLdocument
'sDOMContentLoaded
event listeners. – jave.web Commented Aug 20, 2022 at 10:43
1 Answer
Reset to default 9I came back to this after some time and it started to make sense:
the
Window
listener happens AFTER theDocument
event - because it bubbles FROM the Documentso if you run (jsfiddle):
window.addEventListener("DOMContentLoaded", (event) => { alert("Catching event on window - target: " + event.target.constructor.name); });
...the output in the alert will be:
Catching event on window - target: HTMLDocument
Which proves it's from Document since
window.constructor.name
is"Window"
This is even described in the Living Standard which reads (as of 2023-06-20)
13.2.7 The end
Once the user agent stops parsing the document, the user agent must run the following steps:
1...
6. Queue a global task on the DOM manipulation task source given the Document's relevant global object to run the following substeps:
- Set the Document's load timing info's DOM content loaded event start time to the current high resolution time given the Document's relevant global object.
- Fire an event named DOMContentLoaded at the Document object, with its bubbles attribute initialized to true.
- ...
And also in the Events
part of the docs:
- Event: DOMContentLoaded
- Interface: Event
- Interesting targets: Document
- Description: Fired at the Document once the parser has finished
There is no mention of DOMContentLoaded
for window
specifically :-)
So to answer the main question
The difference is when you want your handler to be executed.
AFTER ALL
document
DOMContentLoaded
listeners have been executed- => use
window.addEventListener("DOMContentLoaded", ...
- => use
Among all other handlers listening to
DOMContentLoaded
onDocument
- => use
document.addEventListener("DOMContentLoaded", ...
- => use
Sidenote: MDN mentions load
event on the "Document: DOMContentLoaded event" page - that is taken out of context and what they mean is to use load
event listener on Window
- does not have a Document
load
event since it isn't an Element
...
本文标签: javascriptwindow DOMContentLoaded vs document DOMContentLoadedStack Overflow
版权声明:本文标题:javascript - window DOMContentLoaded vs document DOMContentLoaded - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641410a2389941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论