34 lines
868 B
JavaScript
34 lines
868 B
JavaScript
function setCookie(name, value, expireDays) {
|
|
let extra = `; domain=${location.host}; path=/`;
|
|
if (expireDays != null) {
|
|
const d = new Date();
|
|
d.setTime(d.getTime() + (expireDays * 24 * 60 * 60 * 1000));
|
|
extra += `; expires=${d.toGMTString()}`;
|
|
}
|
|
if (/^(https|wss):$/.test(location.protocol)) {
|
|
extra += '; secure';
|
|
}
|
|
document.cookie = `${name}=${encodeURIComponent(value)}${extra}`;
|
|
}
|
|
|
|
function getCookie(name) {
|
|
name += '=';
|
|
const cookies = document.cookie.split(';');
|
|
for (let cookie of cookies) {
|
|
cookie = cookie.trim();
|
|
if (cookie.indexOf(name) === 0) {
|
|
return decodeURIComponent(cookie.substring(name.length));
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function deleteCookie(name) {
|
|
setCookie(name, '', -1);
|
|
}
|
|
|
|
export {
|
|
getCookie,
|
|
setCookie,
|
|
deleteCookie
|
|
} |