9 // Please use with caution
12 // Unique for each copy of jQuery on the page
13 expando: "jQuery" + jQuery.now(),
15 // The following elements throw uncatchable exceptions if you
16 // attempt to add expando properties to them.
23 data: function( elem, name, data ) {
24 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
28 elem = elem == window ?
32 var id = elem[ jQuery.expando ], cache = jQuery.cache, thisCache,
33 isNode = elem.nodeType,
36 if ( !id && typeof name === "string" && data === undefined ) {
40 // Get the data from the object directly
45 // Compute a unique ID for the element
47 elem[ jQuery.expando ] = id = ++jQuery.uuid;
50 // Avoid generating a new cache unless none exists and we
51 // want to manipulate it.
52 if ( typeof name === "object" ) {
54 cache[ id ] = jQuery.extend(true, {}, name);
56 store = jQuery.extend(true, {}, name);
57 cache[ id ] = function() {
62 } else if ( !cache[ id ] ) {
67 cache[ id ] = function() {
74 thisCache = isNode ? cache[ id ] : cache[ id ]();
76 // Prevent overriding the named cache with undefined values
77 if ( data !== undefined ) {
78 thisCache[ name ] = data;
81 return typeof name === "string" ? thisCache[ name ] : thisCache;
84 removeData: function( elem, name ) {
85 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
89 elem = elem == window ?
93 var isNode = elem.nodeType,
94 id = elem[ jQuery.expando ], cache = jQuery.cache;
95 if ( id && !isNode ) {
98 var thisCache = cache[ id ];
100 // If we want to remove a specific section of the element's data
103 // Remove the section of cache data
104 delete thisCache[ name ];
106 // If we've removed all the data, remove the element's cache
107 if ( jQuery.isEmptyObject(thisCache) ) {
108 jQuery.removeData( elem );
112 // Otherwise, we want to remove all of the element's data
114 if ( jQuery.support.deleteExpando || !isNode ) {
115 delete elem[ jQuery.expando ];
117 } else if ( elem.removeAttribute ) {
118 elem.removeAttribute( jQuery.expando );
121 // Completely remove the data cache
130 data: function( key, value ) {
131 if ( typeof key === "undefined" && this.length ) {
132 return jQuery.data( this[0] );
134 } else if ( typeof key === "object" ) {
135 return this.each(function() {
136 jQuery.data( this, key );
140 var parts = key.split(".");
141 parts[1] = parts[1] ? "." + parts[1] : "";
143 if ( value === undefined ) {
144 var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
146 // Try to fetch any internally stored data first
147 if ( data === undefined && this.length ) {
148 data = jQuery.data( this[0], key );
150 // If nothing was found internally, try to fetch any
151 // data from the HTML5 data-* attribute
152 if ( data === undefined && this[0].nodeType === 1 ) {
153 data = this[0].getAttribute( "data-" + key );
155 if ( data != null ) {
156 data = data === "true" ? true :
157 data === "false" ? false :
158 data === "null" ? null :
159 rnum.test( data ) ? parseFloat( data ) :
165 return data === undefined && parts[1] ?
166 this.data( parts[0] ) :
170 return this.each(function() {
171 var $this = jQuery( this ), args = [ parts[0], value ];
173 $this.triggerHandler( "setData" + parts[1] + "!", args );
174 jQuery.data( this, key, value );
175 $this.triggerHandler( "changeData" + parts[1] + "!", args );
180 removeData: function( key ) {
181 return this.each(function() {
182 jQuery.removeData( this, key );