/*jslint browser: true */ /*global jquery: true */ /** * jquery cookie plugin * * copyright (c) 2010 klaus hartl (stilbuero.de) * dual licensed under the mit and gpl licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ // todo jsdoc /** * create a cookie with the given key and value and other optional parameters. * * @example $.cookie('the_cookie', 'the_value'); * @desc set the value of a cookie. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true }); * @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 null as value. keep in mind that you have to use the same path and domain * used when the cookie was set. * * @param string key the key 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. * 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. * @option string path the value of the path atribute of the cookie (default: path of page that created the cookie). * @option string domain the value of the domain attribute of the cookie (default: domain of page that created the cookie). * @option boolean secure if true, the secure attribute of the cookie will be set and the cookie transmission will * require a secure protocol (like https). * @type undefined * * @name $.cookie * @cat plugins/cookie * @author klaus hartl/klaus.hartl@stilbuero.de */ /** * get the value of a cookie with the given key. * * @example $.cookie('the_cookie'); * @desc get the value of a cookie. * * @param string key the key of the cookie. * @return the value of the cookie. * @type string * * @name $.cookie * @cat plugins/cookie * @author klaus hartl/klaus.hartl@stilbuero.de */ jquery.cookie = function (key, value, options) { // key and at least value given, set cookie... if (arguments.length > 1 && string(value) !== "[object object]") { options = jquery.extend({}, options); if (value === null || value === undefined) { options.expires = -1; } if (typeof options.expires === 'number') { var days = options.expires, t = options.expires = new date(); t.setdate(t.getdate() + days); } value = string(value); return (document.cookie = [ encodeuricomponent(key), '=', options.raw ? value : encodeuricomponent(value), options.expires ? '; expires=' + options.expires.toutcstring() : '', // use expires attribute, max-age is not supported by ie options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : '' ].join('')); } // key and possibly options given, get cookie... options = value || {}; var result, decode = options.raw ? function (s) { return s; } : decodeuricomponent; return (result = new regexp('(?:^|; )' + encodeuricomponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null; };