admin管理员组

文章数量:1323691

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...

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...

Share Improve this question asked Jul 2, 2009 at 21:12 T.T.T.T.T.T. 34.6k47 gold badges135 silver badges172 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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.

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.

本文标签: javascriptSetting a cookie value to a incrementing intStack Overflow