Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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,273 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');

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,273 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) ?


Navid Mirzaie Milani
Navid Mirzaie Milani
6,273 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,273 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 ?