diff options
Diffstat (limited to 'content/posts/2007-10-17-easycookie-easy-javascript-cookie-management.html')
-rw-r--r-- | content/posts/2007-10-17-easycookie-easy-javascript-cookie-management.html | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/content/posts/2007-10-17-easycookie-easy-javascript-cookie-management.html b/content/posts/2007-10-17-easycookie-easy-javascript-cookie-management.html new file mode 100644 index 0000000..1fcc015 --- /dev/null +++ b/content/posts/2007-10-17-easycookie-easy-javascript-cookie-management.html @@ -0,0 +1,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> + |