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.

3
  • Check this answer. Commented Sep 2, 2013 at 1: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. Commented Sep 2, 2013 at 1:38
  • Check this also Commented Sep 2, 2013 at 7:07

1 Answer 1

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'

Sign up to request clarification or add additional context in comments.

5 Comments

The attributes of my objects are in a working state but not my methods
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: stackoverflow.com/questions/208016/…
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
Not sure what you are trying to do, but maybe you can include jquery to make it a bit easier? stackoverflow.com/questions/4225030/…
So far I'm using $base64 to encode json'd objects and store it in the cookies onbeforeunload....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.