Last active
June 19, 2021 20:13
-
-
Save ShapeGroup/58cd36e37397fdea3916de0a174eedc3 to your computer and use it in GitHub Desktop.
js-kit-cookies-manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//// JS KIT - COOKIE MANAGER | |
//// MIT LICENCE | |
//// Credit: alberto marà | |
// how to use: | |
// var getacookie = cookies.read('mycookiename'); | |
// cookies.write('session', mycookiestringorobject, 10000); | |
// cookies.remove('mycookiename'); | |
class COOKS | |
{ | |
write(ObjName, ObjVal, Timer) | |
{ | |
let time = new Date(); | |
time.setTime(time.getTime() + (parseInt(Timer) * 60000)); | |
document.cookie = ObjName.replace(/ +/g, "") + '=' + (JSON.stringify(ObjVal)) + '; expires=' + (time.toGMTString()) + '; path=/; SameSite=Lax;'; | |
} | |
read(CookieName) | |
{ | |
var cookies = document.cookie.split(";"); | |
for (let i = 0; i < cookies.length; i++) | |
{ | |
let cookedname = cookies[i].split('=')[0].replace(/ +/g, ""), | |
cookedcontent = cookies[i].split('=')[1]; | |
if (cookedname == CookieName) return JSON.parse(cookedcontent); | |
} | |
} | |
remove(CookieName) | |
{ | |
let deletename = CookieName.replace(/ +/g, ""); | |
cookies.write(deletename, '', -1); | |
} | |
} | |
const cookies = new COOKS(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment