Skip to content

Instantly share code, notes, and snippets.

@jimfloss
Created June 14, 2018 15:40
Show Gist options
  • Save jimfloss/e05d35feb9c345f26bdfd6d83f44f663 to your computer and use it in GitHub Desktop.
Save jimfloss/e05d35feb9c345f26bdfd6d83f44f663 to your computer and use it in GitHub Desktop.
JS Cookies
//JS Cookie CRUD
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = null;
} else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start, c_end));
}
return c_value;
}
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function deleteCookie(name) {
setCookie(name, "", -1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment