Util functions for set/get cookie.
This commit is contained in:
@@ -19,4 +19,31 @@ function daysAgo(date) {
|
|||||||
return Math.round(Math.abs((new Date() - new Date(date)) / day));
|
return Math.round(Math.abs((new Date() - new Date(date)) / day));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createCookie(name, value, days) {
|
||||||
|
if (days) {
|
||||||
|
var date = new Date();
|
||||||
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||||
|
var expires = "; expires=" + date.toGMTString();
|
||||||
|
} else var expires = "";
|
||||||
|
|
||||||
|
const domain = `${window.location.hostname}`;
|
||||||
|
console.log("cookie:", `${name}=${value + expires}; path=/; domain=${domain}`);
|
||||||
|
document.cookie = `${name}=${value + expires}; path=/; domain=${domain}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function readCookie(name) {
|
||||||
|
var nameEQ = name + "=";
|
||||||
|
var ca = document.cookie.split(";");
|
||||||
|
for (var i = 0; i < ca.length; i++) {
|
||||||
|
var c = ca[i];
|
||||||
|
while (c.charAt(0) == " ") c = c.substring(1, c.length);
|
||||||
|
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function eraseCookie(name) {
|
||||||
|
createCookie(name, "", -1);
|
||||||
|
}
|
||||||
|
|
||||||
export { dateString, humanReadableDate, daysAgo };
|
export { dateString, humanReadableDate, daysAgo };
|
||||||
|
|||||||
Reference in New Issue
Block a user