/***************************************************************************** * * Copyright (c) 2005-2006 Guido Wesdorp. All rights reserved. * * This software is distributed under the terms of the widgeteer * License. See LICENSE.txt for license text. * * E-mail: guido@infrae.com * ***************************************************************************** cookielib.js - a simple library to deal with cookies in JavaScript usage: var cookies = new cookielib.CookieJar(); cookies.setCookie('foo', 'bar'); // results in 'bar' var foo = cookies.getCookie('foo'); cookies.delCookie('foo'); // results in cookielib.NoSuchCookieException getting thrown var foo = cookies.getCookie('foo'); */ try { var global = window; } catch(e) { var global = this; }; global.cookielib = new function() { /* get and set cookies */ // make the lib available to all scopes within it by using closure var cookielib = this; function CookieJar(doc) { /* simple API to work with cookies */ this._doc = doc || document; this._mapping = undefined; }; this.CookieJar = CookieJar; CookieJar.prototype.setCookie = function(name, value, expires, path, domain) { /* set a cookie all but name and value are optional expires should be (if provided) a string that the Date constructor can grok */ var mapping = this._getMapping(); var cookiestring = name + '=' + escape(value); if (expires) { var exp = new Date(expires); cookiestring += ';expires=' + exp.toGMTString(); }; if (path) { cookiestring += ';path=' + path; }; if (domain) { cookiestring += ';domain=' + domain; }; this._doc.cookie = cookiestring; mapping[name] = value; }; CookieJar.prototype.getCookie = function(name, _default) { /* get a cookie returns _default is a cookie doesn't exist; if no _default is provided either, it raises a cookielib.NoSuchCookieException */ var mapping = this._getMapping(); var value = mapping[name]; if (value === undefined) { if (_default !== undefined) { return _default; }; throw( (new cookielib.NoSuchCookieException(name)) ); }; return value; }; CookieJar.prototype.hasCookie = function(name) { /* checks whether a cookie is available */ var mapping = this._getMapping(); return (mapping[name] !== undefined); }; CookieJar.prototype.delCookie = function(name) { var mapping = this._getMapping(); if (mapping[name] === undefined) { throw( (new cookielib.NoSuchCookieException(name)) ); }; this._doc.cookie = 'name=' + name; delete(this._mapping[name]); }; CookieJar.prototype._getMapping = function() { if (this._mapping !== undefined) { return this._mapping; }; var mapping = this._mapping = this._parseCookieString( this._doc.cookie); return mapping; }; CookieJar.prototype._parseCookieString = function(cookiestring) { /* parse the string that contains the cookies for this document */ var cookies = this._doc.cookie; var mapping = {}; if (!cookies) { return mapping; }; var pairs = cookies.split(';'); for (var i=0; i < pairs.length; i++) { var nv = pairs[i].split('='); var name = nv[0]; var value = unescape(nv[1]); mapping[name] = value; }; return mapping; }; this.NoSuchCookieException = function(name) { /* raised if a cookie doesn't exist */ if (name !== undefined) { this._initialize('NoSuchCookieException', name); }; }; // provide our own _initialize for when jsbase is not available this.NoSuchCookieException.prototype._initialize = function(name, message) { this.toString = function() { return name; }; }; // make it 'subclass' from jsbase's exception.Exception if it is available, // will override _initialize() if executed if (global && global.exception && global.exception.Exception) { this.NoSuchCookieException.prototype = new exception.Exception; }; };