jquery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
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;
}
};
//使用方法
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: '111cn.net', secure: true });