Help With Cookies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SkeetSkeet
    Confirmed User
    • Oct 2005
    • 5404

    #1

    Help With Cookies

    Asking help for a friend :


    I've been having problems setting up a simple javasript. I've just been trying to get a better understanding of cookies...
    overall problem: I need to add a new value to a cookie.

    The script below has an if statement that will either add to the cookie or start a new cookie.

    Example:
    if (document.cookie){
    CODE IF COOKIE EXISTS
    }else{
    CODE IF COOKIE DOES NOT EXIST}

    My problem is this:
    I want to add "old_cookie" + "new_value"... however,
    It only adds to the cookie.
    example:
    old_cookie = 1;
    new_value = 2;
    new_cookie = 1 + 2;
    // I want it to return 3, however it returns 12...

    Also, if I add another value
    example:
    old_cookie now equals = 12;
    new_value = 1;
    new_cookie = 12 + 1;
    //it will return 121;

    Furthermore, if I replace '+' with any other symbol (ie: *, /, -, etc.) it will work just fine...
    example:
    old_cookie = 2;
    new_value = 3;
    new_cookie = 2 * 3;
    //it returns 6;

    ---------------------------------------
    <script language="JavaScript">
    <!-- This will add item to cart

    function process(){
    if(document.cookie && document.cookie != ""){
    var old_cookie = document.cookie;
    var new_value = document.form.quantity.value;
    var new_cookie = old_cookie + new_cookie;

    document.cookie = escape(new_cookie);
    }
    else{

    var new_data = document.form.quantity.value;
    document.cookie = escape(new_data);
    }

    alert(unescape(document.cookie ));
    }

    function kill_cookie(){
    document.cookie = "";
    }

    -->
    </script>

    ICQ 283633188
  • Angelo22
    Writer
    • Feb 2007
    • 3123

    #2
    looks like it just places the second number after the first one without adding it...

    good luck finding a solution
    MAKE MORE MONEY FROM YOUR WEB TRAFFIC - 15% BONUS

    And contact me if you need high quality translating and writing work done - angelo22 (AT) gmail (DOT) com

    Comment

    • Quickdraw
      Confirmed User
      • Mar 2004
      • 1717

      #3
      if(document.cookie && document.cookie != ""){
      var old_cookie = document.cookie;
      var new_value = document.form.quantity.value;
      var new_cookie = old_cookie + new_cookie;
      A note on the above-- it appears to have new_value as what is in the form, but when adding the old_cookie to new_cookie, in "var new_cookie", it isn't used.

      Anyway, as far as adding those digits, you can either multiply each by one, or use the Number function.
      var new_cookie = old_cookie * 1 + new_value * 1;
      or
      var new_cookie = Number(old_cookie) + Number(new_value);

      Good luck

      Comment

      Working...