How to list all registered events of a DOM node us

2020-07-04 07:57发布

I can add or remove an event handler for a DOM node. Is it possible to find out all the registered events handlers of a given DOM node? I am referring to straight Javascript meaning no frameworks or toolkits like jQuery, dojo, Prototype, GWT, etc. If the answer is no, any reason why? Security issues?

7条回答
看我几分像从前
2楼-- · 2020-07-04 08:24

If your interest is to discover some event, in order to disable it - I came here because of that - I recommend to use the Firebug extension, with Mozilla Firefox. Selecting the part of the document, you are interested in, look at the right panel, the Events tab: you will see all events, and can even disable them.

查看更多
来,给爷笑一个
3楼-- · 2020-07-04 08:27

I know this is an old question, but just in case, for chrome you can use getEventListeners

https://developers.google.com/chrome-developer-tools/docs/commandline-api#geteventlistenersobject

as mentioned here:

https://stackoverflow.com/a/17466308/538752

查看更多
姐就是有狂的资本
4楼-- · 2020-07-04 08:27

Also, in Google Chrome, please select the element and notice the number, it will show you $0 or any other number.

Then in console, type this code and press enter.

getEventListeners($0)

and then you will see the result. Please see the image below for more elaboration.

Google Console showing getEventListeners result

查看更多
劳资没心,怎么记你
5楼-- · 2020-07-04 08:29

Visual Event can show you which events are registered, but it only works with DOM level 0 attached events; the W3C level 2 implementation as well as the Internet Explorer proprietary method are not supported and/or cannot be retrieved.

查看更多
做个烂人
6楼-- · 2020-07-04 08:31

I faced the same problem, landed here, and didn't find an useful answer.

In case you can execute script before addEventListener calls from other parties, you might do something really dirty like:

var obj = something; // Your DOM element you want to watch

var beforeAddEvent = obj.addEventListener;
obj.addEventListener = function() {
  // Do something with arguments here (like storing in an array)
  // arguments[0]: event name
  // arguments[1]: Listener function
  // arguments[3]: eventual options passed

  // If you don't call this, the event listener won't even be attached, it might be also useful in some case
  beforeAddEvent.apply(obj, arguments);
};
查看更多
乱世女痞
7楼-- · 2020-07-04 08:32

DOM Level 3 specifies eventListenerList - however, I'm not aware of any DOM implementation which supports this - or any other reliable way to list the event listeners. It seems to have been an oversight to this point.

查看更多
登录 后发表回答