Ever tried to read and write cookies in Javascript? If you have, then I'll wait until you've stopped frothing at the mouth and pounding your keyboard.
...
Feeling better? Good. I just released the first public version of EasyCookie, a simple cookie library for Javascript. Using EasyCookie is, well, easy. For example, here's how you get a cookie:
// get a cookie
val = EasyCookie.get('my_cookie');
And here's how you set one:
// set a cookie
val = 'a random value that i want to save as a cookie';
EasyCookie.set('my_cookie', val);
And, if you haven't already guessed, here's how your remove a cookie:
// remove a cookie
EasyCookie.remove('my_cookie');
But what about all the extra crap you usually have to fight to get
working, like the domain, path, and expiration? Don't panic!
EasyCookie.set
takes a hash of additional attributes as an optional
third argument. Here's another example of EasyCookie.set
, this time
with the optional hash:
// value to set
val = '99 bottles of beer on the wall, 99 bottles of beer!';
// set a cookie that expires in 10 days, and limit the scope to
// "https://foo.example.com/some/path"
EasyCookie.set('my_cookie', val, {
// expires in 10 days
expires: 10,
// limit cookie to domain 'foo.example.com'
domain: 'foo.example.com',
// limit cookie to path '/some/path'
path: '/some/path',
// restrict cookie to secure pages only
secure: true
});
Checking to see if cookies are enabled just got a whole lot simpler, too:
// are cookies enabled?
enabled = EasyCookie.enabled;
// harass user with annoying dialog about their cookie status
alert('Cookies are ' + (enabled ? 'enabled' : 'not enabled'));
Best of all, EasyCookie is BSD licensed, and the minified version weighs in at a measly 1873 bytes. Anyway, here ya go:
PS. Yes, I realize the link above are to version 0.2.1 of EasyCookie, even though this is the first public release. In internet years the backend to my page is older than your grandmother. Imagine trying to explain how to release software to your grandmother.