Skip to content

Instantly share code, notes, and snippets.

@camilamoreiradev
Created October 10, 2021 22:35
Show Gist options
  • Save camilamoreiradev/e3562078430a396cbb77b9cb82af0c37 to your computer and use it in GitHub Desktop.
Save camilamoreiradev/e3562078430a396cbb77b9cb82af0c37 to your computer and use it in GitHub Desktop.
var Cookies = {
setCookie: function (cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays2460601000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
},
getCookie: function(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
//console.log(c)
}
if (c.indexOf(name) == 0) {
//console.log(c.substring(name.length, c.length));
return c.substring(name.length, c.length);
}
}
return "";
},
deleteCookie: function (name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment