In javaScript, using jQuery library, I need to:
- Take an array of objects.
- Stringify it.
- Save it as a cookie.
- On refresh -> Parse cookie and 'recreate' the array.
Using JSON is easy.
// Write JSON Cookie
var foo = JSON.stringify(myValue);
writeCookie(foo);
// Read [Eat?] JSON Cookie
var foo = JSON.parse(readCookie("myArray"));
if(foo.length) {
myArray = foo;
}
(Note: writeCookie(); readCookie(); Are 2 functions I wrote based on the suggested cookie functions on quirksmode.)
Now, my user base involves a lot of pre-IE8 browsers. (Which won't support these methods). So I want to turn to jQuery to plug in the holes. Parsing JSON is easy:
// Read JSON Cookie with jQuery
var foo = jQuery.parseJSON(readCookie("myArray"));
if(foo.length) {
myArray = foo;
}
My question is how do I write a JSON object to cookie using jQuery (Such that it will work for earlier versions of IE).
Thanks
Update: Still confused why jQuery would offer a parseJSON function and not a writeJSON function?
In javaScript, using jQuery library, I need to:
- Take an array of objects.
- Stringify it.
- Save it as a cookie.
- On refresh -> Parse cookie and 'recreate' the array.
Using JSON is easy.
// Write JSON Cookie
var foo = JSON.stringify(myValue);
writeCookie(foo);
// Read [Eat?] JSON Cookie
var foo = JSON.parse(readCookie("myArray"));
if(foo.length) {
myArray = foo;
}
(Note: writeCookie(); readCookie(); Are 2 functions I wrote based on the suggested cookie functions on quirksmode.)
Now, my user base involves a lot of pre-IE8 browsers. (Which won't support these methods). So I want to turn to jQuery to plug in the holes. Parsing JSON is easy:
// Read JSON Cookie with jQuery
var foo = jQuery.parseJSON(readCookie("myArray"));
if(foo.length) {
myArray = foo;
}
My question is how do I write a JSON object to cookie using jQuery (Such that it will work for earlier versions of IE).
Thanks
Update: Still confused why jQuery would offer a parseJSON function and not a writeJSON function?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 11, 2014 at 9:25 User2User2 5911 gold badge7 silver badges17 bronze badges 6- depending on the size of the array you might want to use localStorage instead as it has a higher length limit – Patrick Evans Commented Apr 11, 2014 at 9:28
- Thanks, the array I'm storing isn't very big. Just storing information like 'Selected units', etc. – User2 Commented Apr 11, 2014 at 9:30
- possible duplicate of Serializing to JSON in jQuery – Jon Commented Apr 11, 2014 at 9:30
- @Jon Meh; That solution is to import another library. I'm already using a couple libraries. Don't really want to add another one. (I prefer not to blindly use other peoples code). I just assumed, because jQuery had a parseJSON function it would have a writeJSON function. Might be me being st000pid. – User2 Commented Apr 11, 2014 at 9:34
- @User2: It doesn't because it doesn't need that functionality. If you need to support IE7 then you have to use a polyfill. It's not the end of the world. – Jon Commented Apr 11, 2014 at 9:44
3 Answers
Reset to default 3It's the native function JSON.stringify
; standard jQuery does not provide a patibility wrapper around it, but browser patibility is not bad (will work on IE >= 8 and everything else).
You can use JSON2 library https://github./douglascrockford/JSON-js for patibility with older browsers.
The best way is to include the polyfill for JSON object.
But if you insist create a method for serializing an object to JSON notation inside the jQuery namespace, you can do something like this:
Serializing to JSON in jQuery