Search code examples
javascripteventslistenersdomcontentloaded

javascript DOMContentLoaded event listeners


I have several scripts in html document and all of them have one DOMContentLoaded event listener. Will javaScript keep a relative order of listeners executions or execute them synchron?


Solution

  • Yes, if you're using addEventListener. As of DOM3 (several years ago), the order in which addEventListener handlers are called on the same source was defined: It's the order in which they were attached. (And of course, they're executed in series, not parallel, because the main UI script in browsers runs on a single thread. JavaScript isn't single-threaded, not even on browsers, but there's only a single main UI thread; if you want to use other threads, you have to do something on purpose [e.g., web workers and such].)