/** * cookie plugin * * copyright (c) 2006 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 * */ /** * create a cookie with the given name 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. * * @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. * 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 name. * * @example $.cookie('the_cookie'); * @desc get the value of a cookie. * * @param string name the name 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(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.toutcstring)) { var date; if (typeof options.expires == 'number') { date = new date(); date.settime(date.gettime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toutcstring(); // use expires attribute, max-age is not supported by ie } var path = options.path ? '; path=' + options.path : ''; var domain = options.domain ? '; domain=' + options.domain : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeuricomponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookievalue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jquery.trim(cookies[i]); // does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookievalue = decodeuricomponent(cookie.substring(name.length + 1)); break; } } } return cookievalue; } };