Friday, April 13th, 2007
CookieJar: JSON Cookies
<p>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:
-
-
var jar = new CookieJar({
-
expires:3600, // seconds
-
path: '/'
-
});
-
-
var dog = {name: 'Jacky', breed: 'Alsatian', age:5};
-
-
jar.put('mydog', dog);
-
mydog = jar.get('mydog');
-
-
alert("My dog's name is " + mydog.name);
-
alert("He is " + mydog.age + " years old");
-
alert("He is an " + mydog.breed);
-
Related Content:











Would be nice if you had posted the link… :)
http://www.lalit.org/lab/jsoncookies/
A link to the download location might be handy.
Here it is:
http://www.lalit.org/lab/jsoncookies/
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.
@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.
Lalit, tonote’s right, json.js will completely break Prototype’s Ajax requests. So you really should move to 1.5.1_rc2.
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)
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 (^-^)
Oh boy, answer is wrong… do I still get to enter?
I wrote a remake of this for Mootools:
http://clientside.cnet.com/code-snippets/cookiejsonjs-a-mootools-version-of-cookiejar/
dear lord- this breaks in IE! BEWARE!
even the author’s example breaks, sooooo watch out!
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
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.