How to find event listeners on a DOM node using JavaScript

前端 未结 4 1321
梦谈多话
梦谈多话 2020-12-29 05:18

I still can\'t belive this is not possible but is there a way to loop through the dom and see all event handlers attached using \'addEventListener\'. This post and many othe

相关标签:
4条回答
  • 2020-12-29 05:47

    On Chrome v53 console I tried:

    getEventListeners(document);

    that returns:

    __proto__: Object

    and sub elements, not what I'm looking for.

    So I've tried:

    getEventListeners(window);

    that returns

    Object {beforeunload: Array[1], load: Array[1]}

    That is what I'm looking for. So I think that the correct approach is the Robin like the bird's way:

    getEventListeners(myDomElement):

    where myDomElement is the targeted object got with standard ways like getElementById etc...

    0 讨论(0)
  • 2020-12-29 05:57

    Of course browsers internally have a list of event listeners, but it is not exposed to page-level JavaScript. For example, Firebug (or Eventbug) probably use nsIEventListenerInfo.

    That being said, this old answer still holds:
    How to find event listeners on a DOM node?

    0 讨论(0)
  • 2020-12-29 06:02

    Console of Chrome has a method that can help you check if a dom node has any event listeners registered, for example to check event listeners attached to the document node use:

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

    getEventListeners(document);
    

    You could recursively iterate over all dom nodes and find all event handlers attached if needed.

    0 讨论(0)
  • 2020-12-29 06:07

    use the following function to fetch the json of registered events;

    getEventListeners(node_name); where node_name can be an element name or its id.

    0 讨论(0)
提交回复
热议问题