function getCookie (name) {
const cookieStr = document.cookie;
let cStart = cookieStr.indexOf(`${name}=`);
let cEnd;
if (cStart !== -1) {
cStart += name.length + 1;
cEnd = cookieStr.indexOf(';', cStart);
if (cEnd === -1) {
cEnd = cookieStr.length;
}
return unescape(cookieStr.slice(cStart, cEnd)) || '';
}
return '';
}
function setCookie (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
return false;
}
var sExpires = '';
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + vEnd;
break;
case String:
sExpires = '; expires=' + vEnd;
break;
case Date:
sExpires = '; expires=' + vEnd.toUTCString();
break;
default:
break;
}
}
if (!sDomain) {
sDomain = location.host === 'example01.com' ? '.example01.com' : '.example02.com'; // eslint-disable-line no-param-reassign
}
document.cookie = encodeURIComponent(sKey) + '=' + encodeURIComponent(sValue) + sExpires + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '') + (bSecure ? '; secure' : '');
return true;
}
// 调用
setCookie('userName', 'Diana' || '', 604800, '/');
getCookie('userName');
Cookie --util
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- /** *设置cookie */ function setCookie(cookieName, cookieVal...
- 什么是session?什么是cookie?session和cookie有什么区别?什么场景适用于session?什...
- cookie: 全称http cookie, 是客户端用来储存数据的方式。优点是兼容性好,缺点是会增加网络流量,容...
- 在进行APP+H5混合开发的时候,一些功能是用native方法实现的,如登陆,一些功能是用H5实现的。所以往往需要...