Bug Tracker

Modify

Ticket #3748 (closed bug: fixed)

Opened 5 years ago

Last modified 5 years ago

[1.3b1] jQuery.data() API changed and does not allow to store "" or null as values anymore

Reported by: cbeyls Owned by:
Priority: major Milestone: 1.3
Component: data Version:
Keywords: Cc: cbeyls
Blocking: Blocked by:

Description

I'm testing jQuery 1.3 beta 1.

In jQuery 1.2.6, it was possible to store empty strings ("") and null as values for jQuery.data(). If the value was not set or unset, jQuery.data() would return undefined.

Example:

var value, el = $("#hello")[0];
value = jQuery.data(el, "test");
// value === undefined

jQuery.data(el, "test", "");
value = jQuery.data(el, "test");
// value === ""

jQuery.data(el, "test", null);
value = jQuery.data(el, "test");
// value === null

jQuery.removeData(el, "test");
value = jQuery.data(el, "test");
// value === undefined

However, in jQuery 1.3 beta 1, storing an empty string ("") makes jQuery.data() return null. Furthermore, if the value was not defined, it also returns null instead of undefined, so it is not possible to know if a null value was actually stored or if no value was stored at all, which makes it practically impossible to store null as value as well.

Same example, different values with 1.3b1:

var value, el = $("#hello")[0];
value = jQuery.data(el, "test");
// value === null

jQuery.data(el, "test", "");
value = jQuery.data(el, "test");
// value === null

jQuery.data(el, "test", null);
value = jQuery.data(el, "test");
// value === null

jQuery.removeData(el, "test");
value = jQuery.data(el, "test");
// value === null

This means that code which stores empty string values will break because it will get a null back instead of a string, and code which relied on the return value being different than undefined to confirm that it was set will also break.

Change History

comment:1 Changed 5 years ago by flesler

  • Cc cbeyls added
  • Resolution set to fixed
  • Status changed from new to closed
  • Component changed from core to data

Fixed at [6010-6012]

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.