3 * https://github.com/carhartl/jquery-cookie
5 * Copyright 2011, Klaus Hartl
6 * Dual licensed under the MIT or GPL Version 2 licenses.
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.opensource.org/licenses/GPL-2.0
11 $.cookie = function(key, value, options) {
13 // key and at least value given, set cookie...
14 if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
15 options = $.extend({}, options);
17 if (value === null || value === undefined) {
21 if (typeof options.expires === 'number') {
22 var days = options.expires, t = options.expires = new Date();
23 t.setDate(t.getDate() + days);
26 value = String(value);
28 return (document.cookie = [
29 encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
30 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
31 options.path ? '; path=' + options.path : '',
32 options.domain ? '; domain=' + options.domain : '',
33 options.secure ? '; secure' : ''
37 // key and possibly options given, get cookie...
38 options = value || {};
39 var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
41 var pairs = document.cookie.split('; ');
42 for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
43 if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined