Quick Event-Handling Suggestion

Today I had to rewrite how some events were being handled in an application. One of the advantages often cited when using DOM level 2 event handling (use of attachEvent or addEventListener) is that you can add multiple handlers for the same event type on the same element. While I do not deny this to be an advantage (I will soon explain why), I feel that this practice should generally be avoided. A couple reasons, 1.) browsers do no guarantee what order the events get fired in (see here) 2.) Even if you're using a framework that ensures the events are fired in a specific order, you may still run into issues with how one handler should possibly not be fired due to certain circumstances.

When do you make use of multiple handles for the same type/element pair? I would say generally only when the events are between separate objects. To keep things modular, it is best to let each object/class handle the events by itself rather than trying to make some super handler.

Comments