newbie newbie - 24 days ago 7
jQuery Question

Cache JSON object using jquery and retrieve it

I want to cache the following JSON object in the page so that I can retrieve it on another page.

var test = {
"lists":["list1","list2","list3","list4","list5"],
"maps": {
"key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1"
},
"number1":123456789,
"numberarray1":[1,2,3,4,5,6,7,8,9],
"string1":"A",
"stringarray1":["A1","B1"]
}


How do I cache it and retrieve it again?

Is this possible? If not are there other ways that I can save my JSON object and then retrieve it on another page.

Thank you.

Answer

save that tasty cookie

$.cookie("test-data", JSON.stringify(test));

get that sweet cookie back

var test =  JSON.parse($.cookie("test-data"));

http://www.electrictoolbox.com/jquery-cookies/