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(true, {}, name);
57 store = jQuery.extend(true, {}, name);
58 cache[ id ] = function() {
63 } else if ( !cache[ id ] ) {
68 cache[ id ] = function() {
75 thisCache = isNode ? cache[ id ] : cache[ id ]();
77 // Prevent overriding the named cache with undefined values
78 if ( data !== undefined ) {
79 thisCache[ name ] = data;
82 return typeof name === "string" ? thisCache[ name ] : thisCache;
85 removeData: function( elem, name ) {
86 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
90 elem = elem == window ?
94 var isNode = elem.nodeType,
95 id = elem[ jQuery.expando ], cache = jQuery.cache;
96 if ( id && !isNode ) {
99 var thisCache = cache[ id ];
101 // If we want to remove a specific section of the element's data
104 // Remove the section of cache data
105 delete thisCache[ name ];
107 // If we've removed all the data, remove the element's cache
108 if ( jQuery.isEmptyObject(thisCache) ) {
109 jQuery.removeData( elem );
113 // Otherwise, we want to remove all of the element's data
115 if ( jQuery.support.deleteExpando || !isNode ) {
116 delete elem[ jQuery.expando ];
118 } else if ( elem.removeAttribute ) {
119 elem.removeAttribute( jQuery.expando );
122 // Completely remove the data cache
131 data: function( key, value ) {
132 if ( typeof key === "undefined" && this.length ) {
133 return jQuery.data( this[0] );
135 } else if ( typeof key === "object" ) {
136 return this.each(function() {
137 jQuery.data( this, key );
141 var parts = key.split(".");
142 parts[1] = parts[1] ? "." + parts[1] : "";
144 if ( value === undefined ) {
145 var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
147 // Try to fetch any internally stored data first
148 if ( data === undefined && this.length ) {
149 data = jQuery.data( this[0], key );
151 // If nothing was found internally, try to fetch any
152 // data from the HTML5 data-* attribute
153 if ( data === undefined && this[0].nodeType === 1 ) {
154 data = this[0].getAttribute( "data-" + key );
156 if ( typeof data === "string" ) {
158 data = data === "true" ? true :
159 data === "false" ? false :
160 data === "null" ? null :
161 rdigit.test( data ) && !isNaN( data ) ? parseFloat( data ) :
162 rbrace.test( data ) ? jQuery.parseJSON( data ) :
172 return data === undefined && parts[1] ?
173 this.data( parts[0] ) :
177 return this.each(function() {
178 var $this = jQuery( this ), args = [ parts[0], value ];
180 $this.triggerHandler( "setData" + parts[1] + "!", args );
181 jQuery.data( this, key, value );
182 $this.triggerHandler( "changeData" + parts[1] + "!", args );
187 removeData: function( key ) {
188 return this.each(function() {
189 jQuery.removeData( this, key );