addEventListner The third argument is useCapture flag . If you set it to true , the handler will be executed when the event moves down to the button element. However, if you set it to false , the handler will fire while the event bubbles up:
capture phase | | / \ bubbling up -----------------| |--| |----------------- | element1 | | | | | | -------------| |--| |----------- | | |element2 \ / | | | | | -------------------------------- | | W3C event model | ------------------------------------------
From: http://www.quirksmode.org/js/events_order.html#link4
In your example, onclick should be executed before the click handler in the body tag . If you want to change the order of execution, you must capture event in the body .
source share