Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Javascript events not handled by other elements

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?

like image 862
Grodriguez Avatar asked Nov 04 '22 00:11

Grodriguez


1 Answers

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

like image 85
Robin Maben Avatar answered Nov 12 '22 13:11

Robin Maben