I was searching for details regarding the order of execution of functions bound to a page event in javascript, for example via an EventListener.
For example, if I bind three functions A(), B() and C(), to the same event (say DOMContentLoaded), what will be the order of execution of these three functions? One-by-one based on the order of addEventListener call in JS code? Or they are executed all simultaneously?
Besides, can I modify this order? For example, to have a function, bound to DOMContentLoaded event listener, to be executed before any other function bound to the same event.
Event handlers are always called in the order in which they were registered.
Once registered, you cannot insert additional handlers ahead of them[*].
[*] unless you are some how able to obtain a list of all the handlers, and their EventListener
objects, and call removeEventListener
to remove them, insert your own, and then reinsert the originals. In practise this is likely to be impossible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With