When I use the IE developer tools, some of the DOM elements have attributes added that are of the form
jQueryXXXXXXXXX=”YY”
where XXXXX is a fairly long digit string and YY is a usually small integer value.
I don’t see these with the DOM inspector on Safari.
Why and when are these added? Is the data useful to me in any way?
This is the jQuery expando attribute, it’s a key on the object used to find it’s entry in
$.cache.$.cacheis used for.data(), event handlers, or anything you want to stick in there, it’s a centralized place to store events (makes firing global events easier/more efficient as well) and one place for cleanup. By carrying only the attribute on the element, it’s not necessary to have a data store on each element which may not clone correctly-cross browser, rather it only maintains this key, and can lookup it’s entry in the$.cacheobject at any point.Let’s take an example:
This will give an “ID” or key of sorts, that key corresponds to the property on the
$.cacheobject that stores this element’s data/events (if it has any data/event handlers). For example if the key was “4”, it would be used internally to access$.cache[4].$.cachecontains all data, event handlers, etc for all elements that were assigned by jQuery. It’s assigned by incrementing the$.uuid(an internal ever climbing ID jquery assigns and increments any time a new object’s added into$.cache).A few extra bits:
The random nature of the name isn’t all that random, the
jQueryXXXXXXXXXXXXXXXXXis justjQuery+ the timestamp then jquery was loaded, to give the attribute a unique hopefully non-colliding name.Why don’t you see it with
.html()?, well because jQuery hides it, it does a regex to strip it out.Note:
$.expandoisn’t exposed in 1.3, only 1.4+.Usage:
Is it useful? Well it can be, for example if you analyze
$.cachein your console, and you see you have a memory leak (no.empty()before many.load()calls, leaving event handlers behind for example). You open your console, and do$.cache, you see 500 entries there, let’s say you want to know which object went with 312, then you can select it, like this:As another example, this:
This is one example that’s handy, typically the average jQuery user does’t need to dive into
$.cacheor how it works, but it is there and available in case you never need to go looking. Just run$.cachein your console, there’s likely a wealth of information about all your handlers that you didn’t know was available 🙂