html - JavaScript cookie error -
today trying learn how use cookies, , going pretty well, until tried make cookie save total amount of seconds have been on page, including previous visits. instead of counting 1, 2, 3, counted 1, 11, 111, 1111. here code:
<!doctype html> <html> <head> <script> 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]; while (c.charat(0)==' ') c = c.substring(1); if (c.indexof(name) == 0) { return c.substring(name.length, c.length); } } return ""; } var value = getcookie('value'); window.setinterval(function(){ value = value + 1; document.getelementbyid("value").innerhtml = value; setcookie("value", value, 365); }, 1000); </script> </head> <body> <p> <span id="value">0</span> </body> </html>
value
string, +
concatenation, not addition. use either:
value++;
(this convert number can increment it)
or
value = parseint(value, 10) + 1;
Comments
Post a Comment