41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
export function setCookie(name, value, expires, path, domain) {
|
|
if (import.meta.env.SSR) return
|
|
var cookie = name.trimEnd() + "=" + escape(value) + ";";
|
|
|
|
if (expires) {
|
|
// If it's a date
|
|
if(expires instanceof Date) {
|
|
// If it isn't a valid date
|
|
if (isNaN(expires.getTime()))
|
|
expires = new Date();
|
|
}
|
|
else
|
|
expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
|
|
|
|
cookie += "expires=" + expires.toGMTString() + ";";
|
|
}
|
|
|
|
if (path)
|
|
cookie += "path=" + path + ";";
|
|
if (domain)
|
|
cookie += "domain=" + domain + ";";
|
|
|
|
document.cookie = cookie;
|
|
}
|
|
|
|
export function getCookie(name) {
|
|
if (import.meta.env.SSR) return
|
|
let cname = name + "=";
|
|
let decodedCookie = decodeURIComponent(document.cookie);
|
|
let ca = decodedCookie.split(';');
|
|
for(let i = 0; i <ca.length; i++) {
|
|
let c = ca[i];
|
|
while (c.charAt(0) == ' ') {
|
|
c = c.substring(1);
|
|
}
|
|
if (c.indexOf(cname) == 0) {
|
|
return c.substring(cname.length, c.length);
|
|
}
|
|
}
|
|
return "";
|
|
} |