Skip to content

Instantly share code, notes, and snippets.

@morales2k
Created May 29, 2014 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morales2k/f2205c42ddc38e77f546 to your computer and use it in GitHub Desktop.
Save morales2k/f2205c42ddc38e77f546 to your computer and use it in GitHub Desktop.
doSomething in js based on wether or not a cookie is set with the getCookie function
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
return unescape(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("MyCookie");
if (myCookie == null) {
// do cookie doesn't exist stuff;
}
else {
// do cookie exists stuff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment