Jquery's simple operations on cookie storage in JSON format

Source: Internet
Author: User

Add two plug-ins

Jquery. Cookie. js

Jquery. jsoncookie. js

$ (Function (){
VaR wowohistory = $. jsoncookie ("wowohistory"); // read the cookie in JSON format
VaR id = $ ("# selectgoodsid"). Val ();
VaR IMG = $ ("# gooddefimg"). Val ();
VaR name = $ ("# productname"). Text ();
If (typeof (wowohistory. Goods) = "undefined") {// determine whether the cookie exists
Wowohistory = {"goods ":[
{"Goodid": ID, "IMG": IMG, "name": name}]
};
$. Jsoncookie ("wowohistory", wowohistory, {path: '/', expires: 20}); // store Cookie in JSON format
}
Else {
VaR flag = false;
For (VAR I = 0; I <wowohistory. Goods. length; I ++ ){
If (wowohistory. goods [I]. goodid = ID ){
Flag = true; break;
}
}
If (! Flag ){
If (wowohistory. Goods. length> = 10 ){
// Delete wowohistory. goods [0]; // It can only be empty and is not deleted.
Wowohistory. Goods. splice (); // Delete the first row
}
VaR TMP = {"goodid": ID, "IMG": IMG, "name": name };
Wowohistory. Goods. Push (TMP); // Add
$. Jsoncookie ("wowohistory", wowohistory, {path: '/', expires: 20}); // save
}
}
})
Function clearhistory (){
$. Cookie ("wowohistory", null, {path: '/'}); // Delete cookie
$ ("# Wowohistoryshow" ).css ("display", "NONE ");
}

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.