[whatwg] Detecting eventListeners

Kyle Huey me at kylehuey.com
Thu May 24 20:38:48 PDT 2012

On Thu, May 24, 2012 at 8:27 PM, Glenn Maynard <glenn at zewt.org> wrote:

> Making event listeners enumerable would remove that property.

As I read it, he is not asking to enumerate event listeners.  Simply to
know if there are any for the event in question.  Something like

partial interface EventTarget {
  boolean hasEventListener(DOMString type);

> On Thu, May 24, 2012 at 10:07 PM, Xavier Ho <contact at xavierho.com> wrote:
> > A very common use-case is to record a mouse click on a DOM element which
> > may fire an event on the page.  We want to capture clicks that actually
> > triggered an event, does a HTTP request, and so on, but not meaningless
> > clicks on an empty region.
> >
> > That said, there is no way of surely determining if a click is
> meaningful.
> >  We check if the DOM element clicked on is a button, a link (has href),
> has
> > onclick attribute set, and so on.  However, this will fail on sites that
> > binds 'click' via 'addEventListener' on a strange element, like a <span>
> or
> > a <em> tag.
> >
> This will also fail if the event handler is up the node tree.  That's very
> common with the event delegation pattern, where a capturing event listener
> for is registered for a container object (even on the document).  You have
> no way of knowing whether it'll actually do anything for any particular
> element.  I don't think this is a realistic use case.

Yes, you would need to check the entire event dispatch chain for
listeners.  At that point it may be easier just to fire the event and let
the browser figure it out.  It's also possible that event dispatch of an
event that never "heard" by script in the page can trigger dispatch of an
event that is "heard" by script.  For example, a click event that bubbles
to a <label> element generates a synthetic click event on whatever the
<label> points to.  It is possible for a page to have no event listeners
that see the first click, but have event listeners that see the second.  I
think you really need to let the browser run event dispatch for all events

- Kyle

More information about the whatwg mailing list