Re: [whatwg] Detecting eventListeners

On Fri, 25 May 2012, Xavier Ho wrote:
> 
> We're working on a project that requires detection of registered event 
> listeners.  Our targets are old-style "onclick" attribute bindings, 
> events registered via "addEventListener" (and the IE equivalent), and 
> other custom event libraries such as jQuery's.
> 
> As far as we can tell, there is no way to determine if an element has an 
> eventListener attached to it, created via "addEventListener".  There is 
> a sure way to remove an event (via "removeEventListener"), but we want 
> to enter some code path if and only if an element has an event 
> registered, without altering its eventListener.  This is currently not 
> possible.
> 
> Many discussions about this topic has been raised in the past.  This 
> Stackoverflow answer has a good summary: 
> http://stackoverflow.com/questions/7810534/have-any-browsers-implemented-the-dom3-eventlistenerlist
> 
> As far as the author could tell, this feature was never implemented due 
> to a lack of a use-case.  We have a use-case.

What specifically is your use case?


The main reason this isn't supported is that it breaks the event model: 
the idea of events is that listeners don't affect whether something is 
dispatched or not. You should be able to attach a no-op listener for every 
event type to event event target and there should be no noticeable effect.


On Thu, 24 May 2012, Glenn Maynard wrote:
> On Thu, May 24, 2012 at 10:07 PM, Xavier Ho <contact@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.

I don't see how enumerating event listeners would let you distinguish 
these cases.


> > 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.

Indeed. I often just set my mouse listeners on the Window object and check 
the target manually (since that allows me to detect when the mouse left 
the target unexpectedly too), and you wouldn't be able to catch that case 
by enumerating listeners.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Wednesday, 13 June 2012 23:29:09 UTC