I'm using the jquery.cookie.js plugin from the jQuery SVN and I adjusted it (see patch below) to allow me to...
1. easily delete a cookie with the (IMHO more intuitive) $.cookie("name", null) calls instead of the $.cookie("name", '', { expires: -1 }) calls. 2. specify the expiry time with units as in $.cookie("name", "value", { expires: "2m" }) instead of having to pass a full pre-established Date() object. I just want to share it here in case someone finds this also useful. Or someone of the jQuery developers even wants to pick it up and it to the jQuery SVN. Feel free to do whatever you want with it. Yours, Ralf S. Engelschall [EMAIL PROTECTED] www.engelschall.com Index: cookie/jquery.cookie.js --- cookie/jquery.cookie.js (revision 1611) +++ cookie/jquery.cookie.js (working copy) @@ -17,13 +17,17 @@ * @desc Create a cookie with all available options. * @example $.cookie('the_cookie', 'the_value'); * @desc Create a session cookie. + * @example $.cookie('the_cookie', null); + * @desc Delete a cookie by passing a null value. * @example $.cookie('the_cookie', '', {expires: -1}); * @desc Delete a cookie by setting a date in the past. * * @param String name The name of the cookie. * @param String value The value of the cookie. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. - * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. + * @option Time|Number|Date expires Either a time specified as an integer followed by a unit abbreviation character + * (y: years, w: weeks , d: days, h: hours, m: minutes, s: seconds) + * or an integer specifying the expiration date from now on in days or a Date object. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. * If set to null or omitted, the cookie will be a session cookie and will not be retained * when the the browser exits. @@ -55,15 +59,34 @@ jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; + if (value === null) { + value = ""; + options.expires = -1; + } var expires = ''; - if (options.expires && (typeof options.expires == 'number' || options.expires.toGMTString)) { + if (options.expires) { var date; - if (typeof options.expires == 'number') { + if (typeof options.expires == 'string' && options.expires.match(/^[+-]?[0-9]+[ywdhms]$/) !== null) { + var match = options.expires.match(/^([+-]?[0-9]+)([ywdhms])$/); + options.expires = parseInt(match[0], 10) * (({ + "y": (60 * 60 * 24 * 365), + "w": (60 * 60 * 24 * 7), + "d": (60 * 60 * 24), + "h": (60 * 60), + "m": (60), + "s": (1) + }[match[1]]) || 0); date = new Date(); - date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); - } else { + date.setTime(date.getTime() + options.expires * 1000); + } + else if (typeof options.expires == 'number') { + date = new Date(); + date.setTime(date.getTime() + options.expires * 24 * 60 * 60 * 1000); + } + else if (typeof options.expires.toGMTString != 'undefined') date = options.expires; - } + else + throw "invalid \"expires\" option"; expires = '; expires=' + date.toGMTString(); // use expires attribute, max-age is not supported by IE } var path = options.path ? '; path=' + options.path : '';