aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2007-10-17-easycookie-easy-javascript-cookie-management.html
blob: 1fcc0155b40b6cdd268792a92e463861ff80298e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
date: "2007-10-17T02:28:43Z"
title: 'EasyCookie: Easy Javascript Cookie Management'
---

<p>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.</p>

<p>...</p>

<p>Feeling better?  Good.  I just released the first public version of
<a href="http://pablotron.org/software/easy_cookie">EasyCookie</a>, a simple cookie library for Javascript.  Using
EasyCookie is, well, easy. For example, here's how you get a cookie:</p>

<pre><code>// get a cookie
val = EasyCookie.get('my_cookie');
</code></pre>

<p>And here's how you set one:</p>

<pre><code>// set a cookie
val = 'a random value that i want to save as a cookie';
EasyCookie.set('my_cookie', val);
</code></pre>

<p>And, if you haven't already guessed, here's how your remove a cookie:</p>

<pre><code>// remove a cookie
EasyCookie.remove('my_cookie');
</code></pre>

<p>But what about all the extra crap you usually have to fight to get
working, like the domain, path, and expiration?  Don't panic!
<code>EasyCookie.set</code> takes a hash of additional attributes as an optional
third argument.  Here's another example of <code>EasyCookie.set</code>, this time
with the optional hash:</p>

<pre><code>// 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
});
</code></pre>

<p>Checking to see if cookies are enabled just got a whole lot simpler,
too:</p>

<pre><code>// are cookies enabled?
enabled = EasyCookie.enabled;

// harass user with annoying dialog about their cookie status
alert('Cookies are ' + (enabled ? 'enabled' : 'not enabled'));
</code></pre>

<p>Best of all, EasyCookie is <a href="http://opensource.org/licenses/bsd-license.php">BSD licensed</a>, and the <a href="http://crockford.com/javascript/jsmin.html">minified</a>
version weighs in at a measly 1873 bytes.  Anyway, here ya go:</p>

<ul>
<li><a href="http://pablotron.org/files/easy_cookie-0.2.1.tar.gz">EasyCookie 0.2.1 (tarball)</a> (<a href="http://pablotron.org/files/easy_cookie-0.2.1.tar.gz.asc">Signature</a>)</li>
<li><a href="http://hg.pablotron.org/easy_cookie">EasyCookie Mercurial Repository</a></li>
</ul>

<p>
  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.
</p>