/**
* COOKIE赋值
* @param {string} cName cookie名
* @param {int} value cookie值
* @param {int} exdays cookie有效期
* @param {string} path cookie路径
* @returns
*/
function setCookie(cName, value, exdays, path = '/') {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var cValue = encodeURIComponent(value) + ((exdays == null) ? '' :
'; expires=' + exdate.toUTCString()) + '; path=' + path;
document.cookie = [cName, '=', cValue].join('');
}
/**
* COOKIE取值
* @param {string} cName cookie名
*/
function getCookie(cName) {
var i, x, y, ARRcookies = document.cookie.split(';');
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf('='));
y = ARRcookies[i].substr(ARRcookies[i].indexOf('=') + 1);
x = x.replace(/^\s+|\s+$/g, '');
if (x === cName) {
return decodeURIComponent(y);
}
}
}
JS的Cookie操作方法
最新推荐文章于 2022-11-08 09:57:21 发布