I'm trying to figure out how the DOM keeps track of event handlers, whether bound by using jQuery, addEventListener, or by HTML attribute (e.g. onload="myFunc()").
I've read that jQuery uses the .data() way to store event handlers bound by jQuery... but what about the others? Where do they go? I know that Webkit's inspector tool shows the Event Listeners by inspecting an element in the Elements tab, but where is it getting that information?
Incidentally, in some testing using Chrome's web inspector, I used the console to replace a version of jQuery on a live site with a newer one by pulling in the <script>
tag, thus overriding the jQuery
and $
variables. Obviously, events bound using jQuery before the replacement were lost, because a new .data() interface was introduced in the process.
Those handlers which are "lost," however, are still bound to certain events because they actually end up being called when the event fires. Suppose I want to remove those entirely, or supersede them with my own handlers? This is why I'd like to know how to access the actual handlers wherever the DOM is keeping them... and without jQuery.
They're stored internally. effectively becomes this: a_element. onclick = function(event) { alert("foo"); };
Its stored in the actual list (array) of Event listeners for body . Elements have a list of function references in them for their event listeners. These references are not in the DOM. When firing an event, the browser has to run thru all the appropriate elements looking for these references and running them in order.
You can register event handlers for either phase, bubbling or capturing, by using the function addEventListener(type, listener, useCapture) . If useCapture is set to false , the event handler is in the bubbling phase. Otherwise it's in the capture phase. The order of the phases of the event depends on the browser.
Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.
Regarding methods like addEventListener
, they're not directly visible in regular JavaScript code. They're stored internally.
Regarding inline handlers, they're simply stored directly on the DOM Element, like a typical handler, so that this:
<a href="#" onclick='alert("foo");'>click</a>
effectively becomes this:
a_element.onclick = function(event) { alert("foo"); };
(Older IE doesn't include the event
parameter in the function.)
Regarding jQuery, you're right that they're stored in .data()
, or more accurately jQuery.cache
.
But your handlers are never directly assigned to the element. jQuery assigns a single generic handler (using addEventListener
or attachEvent
, whatever's available) that you never see. When an event occurs, it looks at event.type
, then looks up the element's .data()
to see if there are handlers for that type, and if so, invokes them.
So if you have some script that is overwriting jQuery.cache
, you've effectively orphaned those handlers. You can't remove a handler bound with addEventListener
unless you have a reference to that handler. Since jQuery's generic handler was also stored in jQuery.cache
, there's no way to unbind it unless you destroy the element itself.
I don't remember specifically if the generic handler has a reference to jQuery.cache
or just a subset of it. The reference it does hold would have an impact on just how much leaky data there may be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With