Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Client Side Cookie.

Hello all,

I need to know what is the best way to do the following :

I have a list with two elements in the HTML, I need so save them using $.cookie.

What I am doing now is that I am saving the two <li> to the cookie and just retrieve them.

What is the pros and cons for this way? is there any better idea please?

Thanks a lot.

4 Answers

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

Yes definitelyJson to prefer. Here i've a little example for you that can help you.

var myLists = new Array("<li>item1</li>","<li>item2</li>","<li>item3</li>");
var myObject = {};

//store the array in the object
myObject['items'] = myLists;

//create cookie and store the object in it
var myCookie = Cookie.write('mysettings',JSON.encode(myObject));

//read the cookie
var r = Cookie.read("mysettings');

//decode 
var decodeToJson = JSON.decode(r);

now if you console.log(decode) you get your objects ;).

good luck!

this is offcourse an example you should check whats the best way for you to implement it.

Thanks bro.

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

i don't really understand what you mean? u kinda answer your own question. What do you want to achieve?

Navid Mirzaie Milani, don't you see my question ? I asked what is the pros and cons for storing as "HTML" in the cookie then retrieve it? And is there any better idea, so what I mean (is storing the HTML as HTML in the cookie a good idea) ?

Thanks.

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

ooh my bad :( sorry!, well it isn't bad do that but its much better to create an object and store the html in to that object and store the object in the coockie ;) thats much better and more common way to do it :).

EDIT: take for example if you have 100 lists.. then you have to store them sepparetly in to the cookie , the cookie can became very large and having problems to loading it, if you store those in an object it is more flexible.

Thank you so much for that, so you mean adding each <li> into an object, then adding the object to the cookie right?

So when retrieving it I have to go with .each for each li and append the li's to the <ul> ?

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

1: create one object (for example call it: saveLists) 2: store the list in your object 3: store the object in the coockie 4: retrieve the coockie 5: get the object and do whatever you want with it:)

Thank you man, loved your opinion.

Well, another question related to the same area, would you prefer using a JSON Object here for this ?