Friday 26 August 2016

jquery - How to check if any JavaScript event listeners/handlers attached to an element/document?




Tried to search online, but does not look like I can formulate search query properly.



So as simple as it sounds, how can I, either with jquery or just javascript list all the handlers or event listeners that are attached to element(s)/document/window or present in dom.



Just wondering.



Thank you in advance.


Answer



Without jQuery:




if the listeners were added using elem.addEventListener() method, it is not easy to list these listeners. You can override the EventTarget.addEventListener() method by wrapping it with your own. Then you will have the information, what listeners were registered.



var f = EventTarget.prototype.addEventListener; // store original
EventTarget.prototype.addEventListener = function(type, fn, capture) {
this.f = f;
this.f(type, fn, capture); // call original method
alert('Added Event Listener: on' + type);
}



Working example you can find at http://jsfiddle.net/tomas1000r/RDW7F/


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...