4 rbrace = /^(?:\{.*\}|\[.*\])$/;
9 // Please use with caution
12 // Unique for each copy of jQuery on the page
13 // Non-digits removed to match rinlinejQuery
14 expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),
16 // The following elements throw uncatchable exceptions if you
17 // attempt to add expando properties to them.
20 // Ban all objects except for Flash (which handle expandos)
21 "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
25 data: function( elem, name, data ) {
26 if ( !jQuery.acceptData( elem ) ) {
30 elem = elem == window ?
34 var isNode = elem.nodeType,
35 id = isNode ? elem[ jQuery.expando ] : null,
36 cache = jQuery.cache, thisCache;
38 if ( isNode && !id && typeof name === "string" && data === undefined ) {
42 // 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 jQuery.extend( cache, name );
61 } else if ( isNode && !cache[ id ] ) {
65 thisCache = isNode ? cache[ id ] : cache;
67 // Prevent overriding the named cache with undefined values
68 if ( data !== undefined ) {
69 thisCache[ name ] = data;
72 return typeof name === "string" ? thisCache[ name ] : thisCache;
75 removeData: function( elem, name ) {
76 if ( !jQuery.acceptData( elem ) ) {
80 elem = elem == window ?
84 var isNode = elem.nodeType,
85 id = isNode ? elem[ jQuery.expando ] : elem,
87 thisCache = isNode ? cache[ id ] : id;
89 // If we want to remove a specific section of the element's data
92 // Remove the section of cache data
93 delete thisCache[ name ];
95 // If we've removed all the data, remove the element's cache
96 if ( isNode && jQuery.isEmptyObject(thisCache) ) {
97 jQuery.removeData( elem );
101 // Otherwise, we want to remove all of the element's data
103 if ( isNode && jQuery.support.deleteExpando ) {
104 delete elem[ jQuery.expando ];
106 } else if ( elem.removeAttribute ) {
107 elem.removeAttribute( jQuery.expando );
109 // Completely remove the data cache
110 } else if ( isNode ) {
113 // Remove all fields from the object
115 for ( var n in elem ) {
122 // A method for determining if a DOM node can handle the data expando
123 acceptData: function( elem ) {
124 if ( elem.nodeName ) {
125 var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
128 return !(match === true || elem.getAttribute("classid") !== match);
137 data: function( key, value ) {
138 if ( typeof key === "undefined" ) {
139 return this.length ? jQuery.data( this[0] ) : null;
141 } else if ( typeof key === "object" ) {
142 return this.each(function() {
143 jQuery.data( this, key );
147 var parts = key.split(".");
148 parts[1] = parts[1] ? "." + parts[1] : "";
150 if ( value === undefined ) {
151 var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
153 // Try to fetch any internally stored data first
154 if ( data === undefined && this.length ) {
155 data = jQuery.data( this[0], key );
157 // If nothing was found internally, try to fetch any
158 // data from the HTML5 data-* attribute
159 if ( data === undefined && this[0].nodeType === 1 ) {
160 data = this[0].getAttribute( "data-" + key );
162 if ( typeof data === "string" ) {
164 data = data === "true" ? true :
165 data === "false" ? false :
166 data === "null" ? null :
167 !jQuery.isNaN( data ) ? parseFloat( data ) :
168 rbrace.test( data ) ? jQuery.parseJSON( data ) :
178 return data === undefined && parts[1] ?
179 this.data( parts[0] ) :
183 return this.each(function() {
184 var $this = jQuery( this ), args = [ parts[0], value ];
186 $this.triggerHandler( "setData" + parts[1] + "!", args );
187 jQuery.data( this, key, value );
188 $this.triggerHandler( "changeData" + parts[1] + "!", args );
193 removeData: function( key ) {
194 return this.each(function() {
195 jQuery.removeData( this, key );