Skip to content

Instantly share code, notes, and snippets.

@ohiofi
Created February 1, 2018 15:32
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 ohiofi/e60ef73cb92a009f9d95a0c5b95890a1 to your computer and use it in GitHub Desktop.
Save ohiofi/e60ef73cb92a009f9d95a0c5b95890a1 to your computer and use it in GitHub Desktop.
cookie stuff
//Cookie stuff
function setCookie(variablename, variablevalue, exdays) {
var expiredate = new Date();
expiredate.setTime(expiredate.getTime() + (exdays* 24 * 60 * 60 * 1000));
var expires = "expires="+expiredate.toUTCString();
document.cookie = variablename + "=" + variablevalue + ";"+ expires + ";path=/";
console.log(document.cookie);
}
function getCookie(searchVariable) {
var variableName = searchVariable + "=";
// split the cookie string
var cookieArray = document.cookie.split(';');
for(var i = 0; i < cookieArray.length; i++) {
var c = cookieArray[i];
// strip out empty spaces
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
// return only the searchVariable
if (c.indexOf(variableName) == 0) {
return c.substring(variableName.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome back " + user);
mylocation=parseInt(getCookie("mylocation"));
console.log(mylocation);
} else {
setup();
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
checkCookie();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment