1 /** 2 * cookie 3 * @constructor 4 * @param {String} key 键 5 * @param {String} value 值 6 * @example vui.cookie('the_cookie'); 7 */ 8 vui.cookie = function (key, value, options) { 9 // key and value given, set cookie... 10 if (arguments.length > 1 && (value === null || typeof value !== "object")) { 11 options = jQuery.extend({}, options); 12 13 if (value === null) { 14 options.expires = -1; 15 } 16 17 if (typeof options.expires === 'number') { 18 var days = options.expires, t = options.expires = new Date(); 19 t.setDate(t.getDate() + days); 20 } 21 22 return (document.cookie = [ 23 encodeURIComponent(key), '=', 24 options.raw ? String(value) : encodeURIComponent(String(value)), 25 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE 26 options.path ? '; path=' + options.path : '', 27 options.domain ? '; domain=' + options.domain : '', 28 options.secure ? '; secure' : '' 29 ].join('')); 30 } 31 32 // key and possibly options given, get cookie... 33 options = value || {}; 34 var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent; 35 return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null; 36 }; 37 38