Skip to content

Instantly share code, notes, and snippets.

@banesto
Created February 22, 2018 10:00
Show Gist options
  • Save banesto/2275252876039a221cbfe36732877d0a to your computer and use it in GitHub Desktop.
Save banesto/2275252876039a221cbfe36732877d0a to your computer and use it in GitHub Desktop.
// v1:
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) {
return parts.pop().split(";").shift();
} else {
return null;
}
}
// v2:
function getCookie(name) {
var dc = "; " + document.cookie,
prefix = name + "=",
begin = dc.indexOf("; " + prefix);
if (begin == -1) {
return null;
} else {
begin += 2;
var end = dc.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment