As far as I remember, in pure W3C event model (that means, using addEventListener), there is no guarantee that event handlers will be invoked in the order the have been attached.
What about jQuery event model. Can I rely on the order of attaching events?
Be careful while answering, since actually there can be three options here, not two:
- you can not rely on order of attaching events, as you can see from jQuery code.
- you can rely on order of attaching events in current implementation, but, actually, nobody promiss you this will be always supported in future, since order preserving is actually unintentional.
- you can rely on order of attaching events – this is implemented willingly and most probably will stay with us forever, since is intuitive and helps us to use some common patterns, like resource preinitialization and so on.
If you attach the handlers through jQuery, they will be fired in the order in which they were attached. This is documented on the
bindmethod:You’re correct that the DOM Events specification does not define any order for event handlers (link), and in fact most browsers do it one way, IE does it another. The guaranteed order is something jQuery does for you (by attaching just one handler per event per element — its own — and then doing its own dispatching to the real handlers attached via jQuery). Naturally this means that the order in which the jQuery-attached handlers get called, as a block, in relation to handlers attached in another way is not defined.