Discussion on the data API problem of jquery when customizing the attribute of _javascript to JSON format string techniques

Source: Internet
Author: User
The data API implementation of JQuery has the effect of caching
Using IE 7 (ie8+ in the console switch to IE7 mode), when the DOM node has custom data properties, the DOM node is examined to see the properties of the shape like jQuery18305664906559272507, which is the build that gets the custom data from the data store object.

When a custom data property is a JSON format string, if the cached data is modified, the modified data continues to exist in the cache system, which may cause some bugs if not noted
Copy Code code as follows:

<! DOCTYPE html>
<script src= "Http://code.jquery.com/jquery-1.8.3.js" ></script>
<body>
<input type= "hidden" value= "data-json= ' {" A ": 123," B ": 456} ' id= ' TST '/>
<script>
var node = $ ("#tst");
Console.log (node.attr ("Data-json"));
var data = Node.data ("JSON");
Console.log (data);
data.b = ' Hello ';
var data1 = Json.parse (node.attr ("Data-json"));
Console.log (DATA1);
Console.log (' data===data1 ', data===data1);
var data2 = Node.data ("JSON");
Console.log (DATA2);
Console.log (' Data===data2 ', data===data2);
var data3 = Json.parse (node.attr ("Data-json"))//This value is not affected by caching
Console.log (DATA3);
</script>
</body>

Run results
Copy Code code as follows:

{"A": 123, "B": 456}
Object {a:123, b:456}
Object {a:123, b:456}
Data===data1 false
Object {a:123, B: "Hello"}
Data===data2 true
Object {a:123, b:456}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.