Skip to content

Instantly share code, notes, and snippets.

@LayneSmith
Last active August 29, 2015 14:19
Show Gist options
  • Save LayneSmith/50f16a9f81d9c88a6a48 to your computer and use it in GitHub Desktop.
Save LayneSmith/50f16a9f81d9c88a6a48 to your computer and use it in GitHub Desktop.
//JQUERY
//THIS WILL NEED TO SET COOKIE AFTER VOTE
if ($.cookie(thisPage) != "true"){
$.cookie(thisPage, true, { expires: 60 });
alert("Welcome!");
} else {
alert("You've already been here!");
}
//JAVASCRIPT SET THE NEW COOKIE
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}
function getCookie(cname){
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
//PAGE LOADS AND DO THIS
function checkCookie(){
//GET "user" FROM COOKIE CALLED "username"
var user=getCookie("username");
//IF "user" ISNT BLANK THERE'S ALREADY A COOKIE AND WE GOT THE VALUE ABOVE
if (user!="")
{
alert("Welcome again " + user);
}
//IF "user" IS BLANK THEN WE NEED TO SET IT IN THE COOKIE
else
{
user = prompt("Please enter your name:","");
if (user!="" && user!=null)
{
//SET THE COOKIE "username" TO HAVE THE VALUE SET IN "user" AND HAVE IT LAST 30 DAYS
setCookie("username",user,30);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment