JSFiddle - React, Tailwind, and code Playground
by lid0
HTML
simple cookie js (setCookie,getCookie,deleteCookie)
JavaScript
// author lidlanca
function deleteCookie(){
document.cookie = `${key}=; Max-Age=0; path=/; domain=${location.host}`
}
function getCookie(key){
var c= Object.fromEntries( document.cookie.split(/; {1,}/).map(v=>v.split("=",2)))
console.log(c)
if(key === undefined){
return c
}
return c?.[key]
}
function setCookie(key,value,ttl=null){
var maxAge = ttl ? ` Max-Age=${ttl}; ` : ''
var domain = ` domain=${location.host}; `
var cs = `${key}=${value};${maxAge}${domain}`;
console.log(cs)
document.cookie=cs
}
getCookie()
setCookie('light-mode',12,5)
console.log("dark-mode value:", getCookie("dark-mode"))
console.log("light-mode value:", getCookie("light-mode"))
setTimeout(()=>{ getCookie()}, 1000*4)
setTimeout(()=>{ getCookie()}, 1000*6)