0

I'm kind of a beginner in javascript. I parsed my objects to store them in a cookie like this :

JSON.stringify(myObject)

but what do I do to turn the cookie's data back into my object? I'm missing something...

myObject=JSON.parse(myCookieData);

This is working now.

I'm still having a problem though: I store my cookie with onbeforeunload but it sometimes not store it. I tried with onunload too but I have the same problem. Does it take too much time to store the cookie? What is the problem?

Ok so I'm not using base64 anymore, just storing json'd objects in my cookie. My current problem is that the cookie is starting to grow in size badly... Sometimes it is too big to get stored.. How can I minimize the size?

I now use PersistJS. Thanks for your help.

user2090805
  • 19
  • 2
  • 7
  • [Check this answer](http://stackoverflow.com/questions/11171746/reverse-of-json-stringify). – The Alpha Sep 02 '13 at 01:36
  • @remo, to put a link you can use `[link text](link)`, for example, `[HEERA.IT](http://heera.it)` will produce a nice link, edit your comment. – The Alpha Sep 02 '13 at 01:38
  • [Check this also](http://stackoverflow.com/questions/11344531/pure-javascript-store-object-in-cookie) – Remo Harsono Sep 02 '13 at 07:07

1 Answers1

0

Your code should work unless your 'myCookieData' isn't the actual portion of the cookie you care about.

For example try this in a browser console (javascript):

var foo = new Object();
foo.name = "Joe"
JSON.parse(JSON.stringify(foo))
> Object {name: "Joe"}

The result is an object as long as you have the correct 'myCookieData'

Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56
  • The attributes of my objects are in a working state but not my methods – user2090805 Sep 02 '13 at 03:29
  • Yes, you will lose the methods. You can create a new object (that has the methods) and copy the data from the 'new' object (created from the cookie) via inspection -- see here for some ideas: http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object – Arthur Frankel Sep 02 '13 at 14:10
  • Yes that's what I did in the end. It's working fine now, just have some nan problems to fix. Thanks for your help – user2090805 Sep 02 '13 at 17:48
  • Not sure what you are trying to do, but maybe you can include jquery to make it a bit easier? http://stackoverflow.com/questions/4225030/jquery-save-json-data-object-in-cookie – Arthur Frankel Sep 02 '13 at 22:04
  • So far I'm using $base64 to encode json'd objects and store it in the cookies onbeforeunload.... – user2090805 Sep 02 '13 at 22:07