Skip to content

Instantly share code, notes, and snippets.

@qborreda
Created September 20, 2017 15:54
Show Gist options
  • Save qborreda/5cf5304416118a6b67f204b4584c2f58 to your computer and use it in GitHub Desktop.
Save qborreda/5cf5304416118a6b67f204b4584c2f58 to your computer and use it in GitHub Desktop.
Util to get, set, delete cookies
export function setCookie(c_name, value, exdays, domain) {
let exdate = new Date();
let time = exdate.getTime();
time += 3600 * 1000;
exdate.setTime(time);
exdate.setDate(exdate.getDate() + exdays);
let c_value =
escape(value) + (exdays == null ? '' : '; expires=' + exdate.toUTCString());
c_value += domain ? ';domain=' + domain : '';
c_value += ';path=/';
document.cookie = c_name + '=' + c_value;
}
export function getCookie(c_name) {
let i,
x,
y,
ARRcookies = document.cookie.split(';');
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf('='));
y = ARRcookies[i].substr(ARRcookies[i].indexOf('=') + 1);
x = x.replace(/^\s+|\s+$/g, '');
if (x == c_name) {
return unescape(y);
}
}
}
export function deleteCookie(c_name, domain) {
let c_string = c_name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
c_string += domain ? ';domain=' + domain : '';
document.cookie = c_string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment