Do modules prevent the need to use the DOMContentLoaded listener?
defer is present, it specifies that the script is executed when the page has finished parsing, therefore you can guarantee that the script gets access to the nodes without
defer, the scripts will indeed execute after the page has been fully downloaded in the order that they appeared.
You can see here a schema representing the behavior.
In any case, scripts are always executed before
DOMContentLoaded, you can test that theory here :
And unless you remove the alert from the ready state, all scripts will execute before them, otherwise they get executed in the order they appear in the
So you can be sure that all code inside
DOMContentLoaded will be able to access the fully loaded
On an end note, do watch out of
defer's compatibility across all browsers.
defer, don’t block
defer, they don’t block it too.
This can be tested using
As you can see, the page loads before executing any modules, no matter the order.