JavaScript cookies library
I was shocked to discover that finding a JavaScript library to work with browser cookies isn’t as easy as I expected. So I’m rolling my own solution.
This code allows to access cookies directly by name or an alias.
var Cookies = {
aliases: {},
alias: function(alias, name, defaultValue)
{
Cookies.aliases[alias] = name;
Cookies[alias] = function(value, days)
{
if(value == null)
return Cookies.get(name, defaultValue);
else
Cookies.set(name, value, days);
}
},
set: function(name, value, days)
{
name = Cookies.aliases[name] || name;
var expires = '';
if(!isNaN(days))
{
var date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + escape(value) + expires + "; path=/";
},
get: function(name, defaultValue)
{
name = Cookies.aliases[name] || name;
var regex = new RegExp(name + "s*=s*(.*?)(;|$)");
var cookies = document.cookie.toString();
var match = cookies.match(regex);
if(match)
return unescape(match[1]);
return defaultValue;
},
erase: function(name)
{
Cookies.set(name, '', -1);
}
}
// Create an alias for a cookie named 'a'
Cookies.alias('greeting', 'a', 0);
// Set the value
Cookies.greeting(parseInt(Cookies.greeting()) + 1);
// Display the value
alert(
Cookies.greeting() + 'n' +
Cookies.get('a') + 'n' +
Cookies.get('greeting')
);
5 comments.
Agreed it is surprising! Works very well, even in Safari. Thanks for stepping up to the plate and hitting the home run!
Thanks a lot. This code came in very handy!
Hi Alex! I wrote a JavaScript Cookie Handling Library in Nov of 2005 for the same reason that you wrote one–I was dissatisfied with JavaScript’s capabilities in this area and I didn’t like a lot of what I was finding written by other people (which was very little). I found yours today when I was googling to see where my library fell in the search rankings for the terms ‘JavaScript Cookie Library’.
While I won’t link to my library from here without an invitation (as I do not want to spam) I just wanted to say that I like yours a lot. Some specific things in yours are certainly better than my own, though I do have some things in mine that I like better than yours. Specifically, I like that yours uses regular expression to find the value within the document.cookie string as I am using some long and elementary string position method to find the cookie/value. I also like the real OOP feel that yours has. Mine is written in an OOP manner, bu still has a bit of a procedural feel to it. However, in mine I have tried to account for every parameter that I believe the developer should be able to control when setting a cookie including path, domain, and HTTPS only.
I was also wondering what benefit you find in allowing for aliases? I am not critiquing it–just wondering.
Have you released yours under any specific license? I’d love to roll some concepts from yours into mine, but don’t want to be a thief. If you’d like to take a look at mine and offer some critique on it, I can email you a link or post it here in the comments. Just let me know.
Thanks a lot,
Jim
Though this post is pretty much old. But i can help to bless Alex. Simply fantastic.
Live logn
Sujoy
Hi,
this seems not to work in Chrome … just FYI.
Cheers!
Leave a Reply