Can I rely on a prefix to handle events?

As far as I remember, in a pure W3C event model (which means using addEventListener) there is no guarantee that event handlers will be called in the order in which they are attached.

How about a jQuery event model. Can I rely on the order in which events join? Be careful when answering, as there can actually be three options, not two:

  • you cannot rely on the order in which events are attached, as you can see from jQuery code.
  • you can rely on the order of event attachment in the current implementation , but in fact no one promised that it would always be supported in the future, since saving orders is actually unintentional.
  • you can rely on the procedure for joining events - this is done voluntarily and is likely to stay with us forever, as it is intuitive and helps us use some common templates, such as initializing resources, etc.
+4
source share
1 answer

If you attach handlers via jQuery, they will be launched in the order in which they were attached. This is described in the bind method :

When an event reaches an element, all handlers associated with this type of event for the element are fired. If multiple handlers are registered, they will always be executed in the order in which they were connected. After all handlers are executed, the event continues along the path of propagation of the normal event.

You are right that the DOM Events specification does not specify the order for event handlers ( link ), and in fact, most browsers do this in one case, IE does it differently. A guaranteed order is what jQuery does for you (by attaching only one handler to the event per element - its own - and then its own dispatcher for real handlers connected via jQuery). Naturally, this means that the order in which the handlers associated with jQuery are called as a block is not defined with respect to handlers attached in a different way.

+5
source

Source: https://habr.com/ru/post/1391823/


All Articles