4 rbrace = /^(?:\{.*\}|\[.*\])$/,
10 // Please use with caution
13 // Unique for each copy of jQuery on the page
14 expando: "jQuery" + jQuery.now(),
16 // The following elements throw uncatchable exceptions if you
17 // attempt to add expando properties to them.
24 data: function( elem, name, data ) {
25 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
29 elem = elem == window ?
33 var id = elem[ jQuery.expando ], cache = jQuery.cache, thisCache,
34 isNode = elem.nodeType,
37 if ( !id && typeof name === "string" && data === undefined ) {
41 // Get the data from the object directly
46 // Compute a unique ID for the element
48 elem[ jQuery.expando ] = id = ++jQuery.uuid;
51 // Avoid generating a new cache unless none exists and we
52 // want to manipulate it.
53 if ( typeof name === "object" ) {
55 cache[ id ] = jQuery.extend(cache[ id ], name);
58 store = jQuery.extend(cache[ id ], name);
59 cache[ id ] = function() {
64 } else if ( !cache[ id ] ) {
70 cache[ id ] = function() {
77 thisCache = isNode ? cache[ id ] : cache[ id ]();
79 // Prevent overriding the named cache with undefined values
80 if ( data !== undefined ) {
81 thisCache[ name ] = data;
84 return typeof name === "string" ? thisCache[ name ] : thisCache;
87 removeData: function( elem, name ) {
88 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
92 elem = elem == window ?
96 var isNode = elem.nodeType,
97 id = elem[ jQuery.expando ], cache = jQuery.cache;
98 if ( id && !isNode ) {
101 var thisCache = cache[ id ];
103 // If we want to remove a specific section of the element's data
106 // Remove the section of cache data
107 delete thisCache[ name ];
109 // If we've removed all the data, remove the element's cache
110 if ( jQuery.isEmptyObject(thisCache) ) {
111 jQuery.removeData( elem );
115 // Otherwise, we want to remove all of the element's data
117 if ( jQuery.support.deleteExpando || !isNode ) {
118 delete elem[ jQuery.expando ];
120 } else if ( elem.removeAttribute ) {
121 elem.removeAttribute( jQuery.expando );
124 // Completely remove the data cache
133 data: function( key, value ) {
134 if ( typeof key === "undefined" ) {
135 return this.length ? jQuery.data( this[0] ) : null;
137 } else if ( typeof key === "object" ) {
138 return this.each(function() {
139 jQuery.data( this, key );
143 var parts = key.split(".");
144 parts[1] = parts[1] ? "." + parts[1] : "";
146 if ( value === undefined ) {
147 var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
149 // Try to fetch any internally stored data first
150 if ( data === undefined && this.length ) {
151 data = jQuery.data( this[0], key );
153 // If nothing was found internally, try to fetch any
154 // data from the HTML5 data-* attribute
155 if ( data === undefined && this[0].nodeType === 1 ) {
156 data = this[0].getAttribute( "data-" + key );
158 if ( typeof data === "string" ) {
160 data = data === "true" ? true :
161 data === "false" ? false :
162 data === "null" ? null :
163 rdigit.test( data ) && !isNaN( data ) ? parseFloat( data ) :
164 rbrace.test( data ) ? jQuery.parseJSON( data ) :
174 return data === undefined && parts[1] ?
175 this.data( parts[0] ) :
179 return this.each(function() {
180 var $this = jQuery( this ), args = [ parts[0], value ];
182 $this.triggerHandler( "setData" + parts[1] + "!", args );
183 jQuery.data( this, key, value );
184 $this.triggerHandler( "changeData" + parts[1] + "!", args );
189 removeData: function( key ) {
190 return this.each(function() {
191 jQuery.removeData( this, key );