Skip to content

Instantly share code, notes, and snippets.

@gh-o-st
Last active November 11, 2020 17:51
Show Gist options
  • Select an option

  • Save gh-o-st/0981792e3fafc82ba7fba97054b0e1ed to your computer and use it in GitHub Desktop.

Select an option

Save gh-o-st/0981792e3fafc82ba7fba97054b0e1ed to your computer and use it in GitHub Desktop.
const setCookie = function(name,value,days) {
let expires = "";
if (days) {
let date = new Date();
date.setDate(date.getDate() + days);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
};
const getCookie = function(name) {
let nameEQ = name + "=";
let ca = document.cookie.split(';');
for(let i=0;i < ca.length;i++) {
let 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;
};
const forgetCookie = function(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment