复制代码 代码如下:
/*
cookie工具
使用方法:
//存值
var value = 7天;
tools.cookie(day,value, {expires:7}); //将字符串:7天 以 day这个key保存到cookie中5天
//取值
var v = tools.cookie(day); //用 day 这个key从cookie取出值
*/
tools.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.togmtstring)) {
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.togmtstring(); // 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 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;
}
};