Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are Javascript event handlers stored?

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.

like image 601
Matt Avatar asked Oct 05 '12 22:10

Matt


People also ask

Where are event handlers stored?

They're stored internally. effectively becomes this: a_element. onclick = function(event) { alert("foo"); };

Where are event listeners stored JavaScript?

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.

How are event handlers registered in JavaScript?

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.

Where do I find Windows event listeners?

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.


1 Answers

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.

like image 99
I Hate Lazy Avatar answered Sep 28 '22 14:09

I Hate Lazy