Using Cookies to set an alert in javascript -
i'm trying create script when user opens site or welcomed prompt , after alert welcome entered in prompt set cookie 1 minute problems i'm facing are:
1. alert not show up
2. cookie not set
function checkcookie() { var username=getcookie("username"); if (username!=null && username!="") { alert("welcome again "+ username); } else { username=prompt("please input name:",""); if (username !=null && username!="") { setcookie("username", username, 365); } else { alert("error"); } } } function setcookie (cname,cvalue,exdays) { var d = new date(); d.settime (d.gettime()+ (10*1000)); var expires ="; expires= "+ d.togmtstring(); document.cookie = cname + "=" + cvalue + "; " + expires; +"; path /"; } function getcookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i<ca.length; i++){ var c = ca[i]; while (c.charat(0)==' ') { c = c.substring(1); } if (c.indexof(name) == 0){ return c.substring(name.length, c.length); } } return ""; }
try replacing var expires ="; expires= "+ d.togmtstring();
var expires ="; expires= "+ d.toutcstring();
the expire time saved unix timestamp.
edit: need execute function checkcookie too. thought obvious.
Comments
Post a Comment