Examples of Using the Bubbling Technique: Global Behaviors
In this example you will see how to define global behaviors for anchors and buttons.
Facts:
Zero or more elements can fire the same behavior (DOM's elements "anchor" or "buttons" with the same classname) ( see the panel 1 )
The position of the elements inside the DOM is not important, neither the moment when a certain element is added to the DOM, once the element is available the behavior will be available too ( see the panel 2 ).
If a certain behavior is not available or is undefined, the event will pass thru the DOM's elements even when the target element has the classname applied ( see the panel 3 ).
If a certain DOM element has more than one behavior (classname applied), all the behaviors will be audited using the definition priority (inclusion order), but as soon as one of the behaviors reclaim the event, the others will be notified and can decide if the event will pass thru or will be reclaimed as a secondary behavior ( see panel 4 )
As you can see in the source, in this page we don't use addListener, or any other event definition utility, we are using the event bubbling technique to catch each event in the top level (document.body) in the DOM.