views:

209

answers:

2

Is there a integer element for a cookie? I need the cookie to increment by 10 everytime a certain button is pushed. How can I start the cookie at 0 the first time and it's next value is based on it's current state.

document.cookie += 10;
gives 10, then 1010, then 101010.
Right I dea but I need integer values-
Need 10, 20, 30, etc...

+5  A: 

document.cookie = parseInt(document.cookie) + 10;

Also, remember that you should never trust raw cookie data or data generated purely by javascript for anything serious as users can modify them as they see fit.

Ben Hughes
A: 

Altogether:

function sub_cookie()
{
    if(!document.cookie)
     document.cookie = 0;

    document.cookie = parseInt(document.cookie) + 10;
    //alert(document.cookie);
    document.next.submit();

}

First time through the cookie is set to nothing, so it's initialized to 0. We can then continue incrementing the current state of the cookie, every time the function is called.

The parseInt() function parses a string and returns an integer.

Tommy