I am trying to implement cookies on a webpage. I am having trouble getting it to function properly. I am wanting to store the value of some variables as well. I realize this is very broad, but I know little to nothing about JavaScript cookies and I am working off the w3schools examples. This is what I have so far:
var days=365;
function setCookie(child,user,days) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + days);
var child=escape(user) + ((365==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=child + "=" + child;
}
function getCookie(child) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==child) {
return unescape(y);
}
}
}
function checkCookie() {
var username=getCookie("username");
if (username!=null && username!="") {
alert("Welcome again " + username);
} else {
username=prompt("Please enter your name:","");
if (username!=null && username!="") {
setCookie("username",username,days);
}
}
}