Event order in Client-side
If an element and one of its ancestors have an event handler for the same event, which one should fire first?” Not surprisingly, this depends on the browser.
The basic problem is very simple. Suppose you have a element inside an element
-----------------------------------
| element1 |
| ------------------------- |
| |element2 | |
| ------------------------- |
| |
-----------------------------------
and both have an onClick event handler. If the user clicks on element2 he causes a click event in both element1 and element2. But which event fires first? Which event handler should be executed first? What, in other words, is the event order?
Answers
•Netscape said that the event on element1 takes place first. This is called event capturing.
•Microsoft maintained that the event on element2 takes precedence. This is called event bubbling.
The two event orders are radically opposed. Explorer only supports event bubbling. Mozilla, Opera 7 and Konqueror support both. Older Opera's and iCab support neither.
Event capturing
When you use event capturing
| |
---------------| |-----------------
| element1 | | |
| -----------| |----------- |
| |element2 \ / | |
| ------------------------- |
| Event CAPTURING |
-----------------------------------
the event handler of element1 fires first, the event handler of element2 fires last.
Event bubbling
When you use event bubbling
/ \
---------------| |-------------
| element1 | | |
| -----------| |--------- |
| |element2 | | | |
| ----------------------- |
| Event BUBBLING |
-------------------------------
the event handler of element2 fires first, the event handler of element1 fires last.
The basic problem is very simple. Suppose you have a element inside an element
-----------------------------------
| element1 |
| ------------------------- |
| |element2 | |
| ------------------------- |
| |
-----------------------------------
and both have an onClick event handler. If the user clicks on element2 he causes a click event in both element1 and element2. But which event fires first? Which event handler should be executed first? What, in other words, is the event order?
Answers
•Netscape said that the event on element1 takes place first. This is called event capturing.
•Microsoft maintained that the event on element2 takes precedence. This is called event bubbling.
The two event orders are radically opposed. Explorer only supports event bubbling. Mozilla, Opera 7 and Konqueror support both. Older Opera's and iCab support neither.
Event capturing
When you use event capturing
| |
---------------| |-----------------
| element1 | | |
| -----------| |----------- |
| |element2 \ / | |
| ------------------------- |
| Event CAPTURING |
-----------------------------------
the event handler of element1 fires first, the event handler of element2 fires last.
Event bubbling
When you use event bubbling
/ \
---------------| |-------------
| element1 | | |
| -----------| |--------- |
| |element2 | | | |
| ----------------------- |
| Event BUBBLING |
-------------------------------
the event handler of element2 fires first, the event handler of element1 fires last.
Comments