Friday, April 13th, 2007

CookieJar: JSON Cookies

Category: JavaScript, Library

Lalit Patel has created a JavaScript Library to use JSON to store data in cookies.

JSON Cookies is built on top of Prototype and gives you a simple API to put and get JSON values into cookies:

javascript

  1. var jar = new CookieJar({  
  2.      expires:3600,   // seconds  
  3.      path: '/'  
  4. });    
  5.  
  6. var dog = {name: 'Jacky', breed: 'Alsatian', age:5};  
  7.    
  8. jar.put('mydog', dog);  
  9. mydog = jar.get('mydog');  
  10.  
  11. alert("My dog's name is " + mydog.name);  
  12. alert("He is " + mydog.age + " years old");  
  13. alert("He is an " + mydog.breed);

Posted by Dion Almaer at 7:11 am
12 Comments

+++--
3.6 rating from 16 votes

12 Comments »

Comments feed TrackBack URI

Would be nice if you had posted the link… :)

http://www.lalit.org/lab/jsoncookies/

Comment by Nathan Toone — April 13, 2007

A link to the download location might be handy.

Here it is:
http://www.lalit.org/lab/jsoncookies/

Comment by Edwin — April 13, 2007

The newest Prototype (1.5.1_rc2) includes json methods in it. This is because json.js extends the “Object object” prototype which is considered very bad and conflicts with some Prototype ajax methods.

Comment by tonote — April 13, 2007

@tonote
Yes I decided to use Prototype 1.5.1_rc2 but then since it is not a stable version, this script wont be of any immediate use to many. So I thought of using json.js and later when a toJSON() gets included in Prototype, I would change this script.

Thanks.

Comment by Lalit Patel — April 13, 2007

Lalit, tonote’s right, json.js will completely break Prototype’s Ajax requests. So you really should move to 1.5.1_rc2.

Comment by Tobie Langel — April 13, 2007

Also for those who do not wish to update to the RC version of Prototype just yet, you can use http://www.stevekallestad.com/blog/making_prototype_compatible_with_jsonjs.html (it allows for json.js to continue its bad behavior and not break Prototype’s ajax methods)

Comment by tonote — April 13, 2007

Why did you created this on the top of prototype? After seeing your code. I think it wouldn’t be too difficult to make it framework independent. But it is nice, I will do experiments with it, thanks (^-^)

Comment by Rizqi Ahmad — April 13, 2007

Oh boy, answer is wrong… do I still get to enter?

Comment by you — April 13, 2007

I wrote a remake of this for Mootools:
http://clientside.cnet.com/code-snippets/cookiejsonjs-a-mootools-version-of-cookiejar/

Comment by Aaron N. — April 16, 2007

dear lord- this breaks in IE! BEWARE!

even the author’s example breaks, sooooo watch out!

Comment by jason — July 27, 2007

I wrote an article on using MooTools’ Hash.Cookie class, which is quite similar:

http://www.thetruetribe.com/2008/05/using-mootools-hashcookie-api.html

Comment by jdempcy — May 8, 2008

Either writing or retrieving from cookies doesn’t seem to work with some IE browsers. Other releases of IE7 seem okay, but things start to break when you use the following IE7:

IE 7.0.5730.11

I’d love to know if there is a fix for this.

Comment by timhysniu — July 31, 2009

Leave a comment

You must be logged in to post a comment.