Skip to content

Instantly share code, notes, and snippets.

@ShapeGroup
Last active June 19, 2021 20:13
Show Gist options
  • Save ShapeGroup/58cd36e37397fdea3916de0a174eedc3 to your computer and use it in GitHub Desktop.
Save ShapeGroup/58cd36e37397fdea3916de0a174eedc3 to your computer and use it in GitHub Desktop.
js-kit-cookies-manager
//// 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