Discussion:
DOML3: ACTION-266: TRAVIS - Test addEventListener vs onFoo attribute
Travis Leithead
2008-06-03 22:01:09 UTC
Permalink
Interesting findings (mostly related to IE):

This test tries to define if the HTML event handlers (onFoo) are linked to the add/removeEventListener APIs in any way (or to define what the relationship is).

Browsers tested: Opera 9.25, Firefox 3 RC1, IE8 Beta1, Safari 3.1.1.

(on IE, I substituted attach/detachEvent for add/removeEventListener)

The findings appear to indicate that:

1. All tested browsers follow a basic model in that an HTML event handler is maintained separately (perhaps in a separate queue) from event handlers attached via programmatic means (e.g., addEventListener/attachEvent). This can be verified by adding an HTML event handler and then trying to delete the reference to its DOM attribute via removeEventListener.

In addition to this basic conclusion, there were a few discrepencies in event handling that should be noted:

* IE/Firefox/Opera all keep a reference alive to the HTML event handler via the element's 'onclick' DOM attribute even after the content attribute has been removed.
* Safari also keeps a reference to the element's 'onclick' handler, yet the "body" of the handler may be missing if the element's content attribute is removed (this prevents adding an event listener via the DOM attribute if the content attribute is missing).
* IE's attachEvent is cumulative, despite re-applying the same handler over and over. detachEvent works in reverse, removing references until none are left.
* IE has a bug that HTML event handlers are not actually removed via removeAttribute (though the attribute itself is removed). This is being fixed in IE8, but does impact this test page :(
Jonas Sicking
2008-06-09 06:34:55 UTC
Permalink
Post by Travis Leithead
This test tries to define if the HTML event handlers (onFoo) are linked to the add/removeEventListener APIs in any way (or to define what the relationship is).
Browsers tested: Opera 9.25, Firefox 3 RC1, IE8 Beta1, Safari 3.1.1.
(on IE, I substituted attach/detachEvent for add/removeEventListener)
1. All tested browsers follow a basic model in that an HTML event
handler is maintained separately (perhaps in a separate queue) from
event handlers attached via programmatic means (e.g.,
addEventListener/attachEvent). This can be verified by adding an HTML
event handler and then trying to delete the reference to its DOM
attribute via removeEventListener.
This is not true. You are assuming that the EventListener object used
for onfoo attributes is the onfoo object itself. This is not the case in
firefox, nor would I expect it to be the case anywhere else. Instead we
create a separate EventListener object which wraps the onfoo object and
deals with things like calling .preventDefault() if the onfoo object
returns false. It also does lazy compilation when onfoo is set via
setAttribute("onfoo", "your code here");

In firefox i think onfoo listeners do go in the same list as other event
listeners, however you can't get to the EventListener object itself, so
you won't be able to remove it using removeEventListener.

A better way to test for this would be something like:

myElem.addEventListener("click", listener1, false);
myElem.onclick = listener2;
myElem.addEventListener("click", listener3, false);

And then check in which order the three listeners are called. If
attribute listeners end up in the same list as "normal" listeners then
the calling order should be: listener1, listener2, listener3.
Post by Travis Leithead
* IE/Firefox/Opera all keep a reference alive to the HTML event handler via the element's 'onclick' DOM attribute even after the content attribute has been removed.
Hmm.. this sounds like a bug to me. I would have expected
.removeAttribute("onclick") to remove the .onclick attribute.

/ Jonas

Loading...