I am writing a script where I would like to handle mouse events only if they have not been handled by any other element before.
I can attach an event listener to the document object, but it will receive all events regardless of whether they have been already handled.
I have no control over the elements in the HTML page so I cannot manually stopPropagation() when the event is handled.
Any ideas?
From this article here.
It seems its not yet possible to do this.
Which event handlers are registered?
One problem of the current implementation of W3C’s event registration model is that you can’t find out if any event handlers are already registered to an element. In the traditional model you could do:
alert(element.onclick)
and you see the function that’s registered to it, or undefined if nothing is registered. Only in its very recent DOM Level 3 Events W3C adds an
eventListenerList
to store a list of event handlers that are currently registered on an element. This functionality is not yet supported by any browser, it’s too new. However, the problem has been addressed.
Fortunately
removeEventListener()
doesn’t give any errors if the event listener you want to remove has not been added to the element, so when in doubt you can always use removeEventListener().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With