Email conversation
From | Cameron M Clark |
To | Me |
Subject | thank you for your excellent article |
Date | 15 August 2008 23:10 |
Tarquin,
I wanted to thank you for your article on DOM events
(http://www.howtocreate.co.uk/tutorials/javascript/domevents). I spent
several hours yesterday trying to figure out an issue with event
assignments, and it wasn't until I finally found your article that I
understood what was going on. For some reason, after hours of Googling, your
site was the first and ONLY place I found a clear explanation of how to
access events, and why you can't access DOM events:
Note that, unlike with the traditional events models, it is not possible to
obtain a reference to the event handler functions (this could previously
done with referenceToElement.oneventname, but DOM 2 events does not provide
any equivalent to list all the event handlers).
(Mind you, I think this situation absolutely ridiculous...all your
dynamically assigned events just go into a black hole, never to be
retrieved? I can't fathom who thought this made sense.)
For some reason, virtually all references to working with events--and
Microsoft's own event object, window.event--seem to just implicitly assume
that you'd only ever want to work with an event if/when it's firing, within
the scope of that event. Those references that do talk about getting events
by object always seem to use the old referenceToElement.oneventname method,
which as you pointed out doesn't work with modern event models, which they
never seem to note.
Then you had a whole section on Manually firing events, which spelled out
exactly how to use Microsoft' fireEvent and the DOM2 createEvent, initEvent,
and dispatchEvent methods to access and execute DOM events by object. This
was a godsend and finally helped me figure out how to solve my issue.
I don't know why it took so darn long to find this little bit of
enlightenment, but at least I had I finally had figured out what was going
on. Thank you for being the only reference I could find on the web on this
strangely murky area of Javascript. Thanks again for a great site!
Cameron
From | Me |
To | Cameron M Clark |
Subject | Re: thank you for your excellent article |
Date | 25 August 2008 08:47 |
Cameron,
> Note that, unlike with the traditional events models, it is not
> possible to obtain a reference to the event handler functions
>
> (Mind you, I think this situation absolutely ridiculous...all your
> dynamically assigned events just go into a black hole, never to be
> retrieved? I can't fathom who thought this made sense.)
Indeed, this is quite an oversight. The only solution I could think of was
to write a wrapper function that adds or removes the event listeners using
the DOM method, but at the same time populates an array attached to the
element object. Each entry contains a copy of the parameters passed to the
addEventListener method. It would need to know how to ignore duplicates as
well. Then add some custom functions which can search through that array for
all listeners for a named event, or event+function or event+function+phase
combination. Not pretty.
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/