How can I get and set Cookies using JavaScript? -
this question has answer here:
i trying implement cookies on page; having trouble getting function properly. wanting store value of variables well. realize broad, know little nothing jscript cookies , working off w3schools examples. if has helpful insight push me along great. have 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(";"); (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 name:",""); if (username!=null && username!="") { setcookie("username",username,days); } } }
yes, have parse error on line 1. matt said, "365" not legal here.
also, looks code never evaluate true...
if (x==child) { return unescape(y); } } ...and cookie value y never returned.
there other things problematic in code too, start i've mentioned here. also, try debugging javascript tools, ie. firebug.
Comments
Post a Comment