Is there any way to view what functions / code are attached to any event for a DOM element? Using Firebug or any other tool.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
You can use Visual Event by Allan Jardine to inspect all the currently attached event handlers from several major JavaScript libraries on your page. It works with jQuery, YUI and several others.
Visual Event is a JavaScript bookmarklet so is compatible with all major browsers.
You can view directly attached events (element.onclick = handler) by looking at the DOM. You can view jQuery-attached events in Firefox using FireBug with FireQuery. There doesn't appear to be any way to see addEventListener-added events using FireBug. However, you can see them in Chrome using the Chrome debugger.
Event handlers attached using traditional
element.onclick= handler
or HTML<element onclick="handler">
can be retrieved trivially from theelement.onclick
property from script or in-debugger.Event handlers attached using DOM Level 2 Events
addEventListener
methods and IE'sattachEvent
cannot currently be retrieved from script at all. DOM Level 3 once proposedelement.eventListenerList
to get all listeners, but it is unclear whether this will make it to the final specification. There is no implementation in any browser today.A debugging tool as browser extension could get access to these kinds of listeners, but I'm not aware of any that actually do.
Some JS frameworks leave enough of a record of event binding to work out what they've been up to. Visual Event takes this approach to discover listeners registered through a few popular frameworks.
The Elements Panel in Google Chrome Developer tools has had this since Chrome releases in mid 2011 and Chrome developer channel releases since 2010.
Also, the event listeners shown for the selected node are in the order in which they are fired through the capturing and bubbling phases.
Hit command + option + i on Mac OSX and Ctrl + Shift + i on Windows to fire this up in Chrome
Chrome Dev Tools recently announced some new tools for Monitoring JavaScript Events.
Finding Custom Events
For my need, discovering custom JS events in 3rd party code, the following two versions of the
getEventListeners()
were amazingly helpful;getEventListeners(window)
getEventListeners(document)
If you know what DOM Node the event listener was attached to you'd pass that instead of
window
ordocument
.Known Event
If you know what event you wish to monitor e.g.
click
on the document body you could use the following:monitorEvents(document.body, 'click');
.You should now start seeing all the click events on the
document.body
being logged in the console.You can extend your javascript environment to keep track of event listeners. Wrap (or 'overload') the native addEventListener() method with some code that cans keep a record of any event listener added from then onwards. You'd also have to extend HTMLElement.prototype.removeEventListener to keep records that accurately reflect what is happening in the DOM.
Just for the sake of illustration (untested code) - this is an example of how you would 'wrap' addEventListener to have records of the registered event listeners on object itself: