5

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.org.)

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?

Community
  • 1
  • 1
User2
  • 581
  • 1
  • 7
  • 17
  • depending on the size of the array you might want to use localStorage instead as it has a higher length limit – Patrick Evans Apr 11 '14 at 09:28
  • Thanks, the array I'm storing isn't very big. Just storing information like 'Selected units', etc. – User2 Apr 11 '14 at 09:30
  • possible duplicate of [Serializing to JSON in jQuery](http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery) – Jon Apr 11 '14 at 09: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 Apr 11 '14 at 09: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 Apr 11 '14 at 09:44
  • @Jon Starting to see that. Yea. Thanks. Will polyfill. #theWorldIsEnding – User2 Apr 11 '14 at 09:46

3 Answers3

3

It's the native function JSON.stringify; standard jQuery does not provide a compatibility wrapper around it, but browser compatibility is not bad (will work on IE >= 8 and everything else).

Jon
  • 428,835
  • 81
  • 738
  • 806
  • But that is not supported by versions of Internet Explorer prior to IE8. jQuery normally has a way around backward compatibility. – User2 Apr 11 '14 at 09:29
  • @User2: That's true. I have found another question of which this is a duplicate where the answers suggest compatibility workarounds. – Jon Apr 11 '14 at 09:31
1

You can use JSON2 library https://github.com/douglascrockford/JSON-js for compatibility with older browsers.

kabanoid
  • 106
  • 3
0

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

Community
  • 1
  • 1
jherax
  • 5,238
  • 5
  • 38
  • 50