Ticket #7241 (closed bug: duplicate)
.data() doesn't parse JSON strings, but accepts all other JSON notation
| Reported by: | leo@… | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.5 |
| Component: | data | Version: | 1.4.3 |
| Keywords: | string support data | Cc: | |
| Blocking: | Blocked by: |
Description
Regarding the broken .data() functionality in jQuery 1.4.3
It is expected that .data() can be used to pass a JSON encoded variable into a data attribute and having it fetched in the same structure with .data() by the client. This is true for null, boolean, number, array and object but not strings!
What I expect: To retain the structure, variables & types of the data.
What actually happens: Strings are either unparsed or accidentally parsed as something else. There is no functionality for accurately retaining the contents of strings.
More info on the problem here: http://www.reddit.com/r/programming/comments/ds42b/jquery_143_released/c12l6z9
And a visual of how the data parsing should work...
When I use:
$("div").data("variable");
The result from .data() should simply be equivalent to this:
try{
var variable = jQuery.parseJSON( $("div").attr("data-variable") );
}catch(e){
var variable = $("div").attr("data-variable");
}
But instead it is not simply parsing JSON first, it is parsing everything except strings, and leaving anything else as raw, unparsed strings.
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

The bug submitted sounds a lot like #7231. I'll reference your ticket in the other thread but will be closing this as a duplicate as both refer to the broken string support.