作者:哒哒愛嘬萌 | 来源:互联网 | 2022-10-13 18:00
example $.COOKIE('name', ‘value');
设置COOKIE的值,把name变量的值设为value
example $.COOKIE('name', ‘value', {expires: 7, path: ‘/', domain: ‘jquery.com', secure: true});
新建一个COOKIE 包括有效期 路径 域名等
example $.COOKIE('name', ‘value');
新建COOKIE
example $.COOKIE('name', null);
删除一个COOKIE
var account= $.COOKIE('name');
取一个COOKIE(name)值给myvar
代码如下
代码如下:
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 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;
}
};
然后看了下Discuz!中对COOKIE的操作方法
如下,发现少了个遍历用;分割的数组的处理
代码如下:
function getCOOKIE(name) {
var COOKIE_start = document.COOKIE.indexOf(name);
var COOKIE_end = document.COOKIE.indexOf(";", COOKIE_start);
return COOKIE_start == -1 ? '' : unescape(document.COOKIE.substring(COOKIE_start + name.length + 1, (COOKIE_end > COOKIE_start ? COOKIE_end : document.COOKIE.length)));
}
function setCOOKIE(COOKIEName, COOKIEValue, seconds, path, domain, secure) {
var expires = new Date();
expires.setTime(expires.getTime() + seconds);
document.COOKIE = escape(COOKIEName) + '=' + escape(COOKIEValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '/')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
}