javascript cookie arithmetic count -


i trying use cookies store numbers , when trying count it's not working.

        var count2 = "2";         var count2 = parseint(count2);         document.cookie=counter1= + count2 + 21;         window.alert(parseint(readcookie('counter1')));         window.alert(count2 + 1); 

first results: 221 second results: 3

the problem when getting count cookie.

that's read function.

function readcookie(name) {     var nameeq = name + "=";     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,c.length);         if (c.indexof(nameeq) == 0) return c.substring(nameeq.length,c.length);     }     return null; } 

i'd store numbers in cookies , convert integers.

if wanted add count2 21, you'll need wrap them in brackets, you're concatenating string numbers parsed strings, hence 2 + 21 = "221", instead of = 23.

try document.cookie="counter1=" + (count2 + 21);


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -