Activate your free membership today | Log-in

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

Posted by Dion Almaer at 7:11 am

+++--
3.7 rating from 9 votes

11 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

Leave a comment

You must be logged in to post a comment.