www.webdeveloper.com
Results 1 to 5 of 5

Thread: Increment/decrement cookie value

  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Increment/decrement cookie value

    Hi all. I'm a novice when it comes to Javascript, so please bear with me if I've done something stupid. I'm trying to make a script that I can use for buttons to increase or decrease a cookie's value by 1. I've tried changing numerous things in the scripts, but nothing has got it working (the scripts for getting and setting the cookie are all working fine).

    Here are my scripts for setting and getting the cookie, respectively:
    Code:
    <script type="text/javascript">
    function getCookie(name) {
    var a = document.cookie;
    var begin = a.indexOf(name + "=");
    var end = (a.indexOf(";", begin) == -1) ? a.length : a.indexOf(";", begin);
    return unescape(a.substring((begin + (name.length + 1)), end));}
    </script>
    Code:
    <script type="text/javascript">
    function setCookie(name, value, expires, path, domain, secure ) {
    if (!name || !value) {return 0;}
    document.cookie = name + "=" + value + ( (expires) ? ";expires=" + expires_date.toGMTString() : "" ) + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : "");
    }
    </script>
    This is my function that I'm using to write on the page what the value of the cookie is:
    Code:
    <script type="text/javascript">
    function wrtCookie(name){
    var x = getCookie(name)
    if (x==4) {
    var line ='Variable is 4'
    document.write(line);}
    else if (x==3) {
    var line ='Variable is 3'
    document.write(line);}
    else if (x==2) {
    var line ='Variable is 2'
    document.write(line);}
    else if (x==1) {
    var line ='Variable is 1'
    document.write(line);}
    else {
    document.write('Cookie is not working');}
    }
    </script>
    And here the two scripts that seem to be the problem, the increase and decrease value scripts (yes, I do mean to have the <=4 and >=1 in there for the variable, since I'd like to restrict it to that range):
    Code:
    <script type="text/javascript">
    function incCookie(name) {
    var z = getCookie(name)
    var j = z++	
    if (j<=4) {
    setCookie('test',j,'4','','/','','');}
    }
    </script>
    Code:
    <script type="text/javascript">
    function decCookie(name) {
    var z = getCookie(name)
    var j = z--
    if (j>=1) {
    setCookie('test',j,'4','','/','','');}
    }
    </script>
    All of those scripts are in my head tag. This is what I have in the body of my page:
    Code:
    <body>
    <script type="text/javascript">wrtCookie('test');</script><br />
    <a href="javascript:location.reload(true)" onClick="setCookie('test','4','','/','','');">Set to 4.</a><br />
    <a href="javascript:location.reload(true)" onClick="incCookie('test');">Add 1</a><br />
    <a href="javascript:location.reload(true)" onClick="decCookie('test');">Subtract 1</a><br />
    </body>
    Does anyone know what I'm doing wrong here? Any help would be greatly appreciated.

  2. #2
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    Your function is:

    function setCookie(name, value, expires, path, domain, secure )

    So in your inc and dec functions change this:

    setCookie('test',j,'4','','/','','');

    to this:

    setCookie(name,j,'','','/','','');

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Thanks for pointing that out. I think I was changing around some stuff in those, since I'd originally written them to only work the the cookie 'test', but got something mixed up. The increase and decrease scripts still aren't working though.

  4. #4
    Join Date
    Oct 2009
    Posts
    3
    Does anyone else have any input on this?

  5. #5
    Join Date
    Apr 2006
    Location
    Houston
    Posts
    1,374
    You can't do this:
    Code:
    <a href="javascript:location.reload(true)" onClick="incCookie('test');">Add 1</a><br />
    You can't run javascript in both the HREF and in the onClick of an anchor tag.

    The way it looks now is that any time you click on the link you are just reloading the current page and doing nothing else.

    If you need to do multiple functions put them all in the onClick separated by semi-colons (I don't know why you need to do page reload here anyway).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles