cookie example - where's the bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Asad

    cookie example - where's the bug?

    I am trying to get a cookie going on my site. Here is the code first:

    function setCookie()
    {
    if(document.coo kie == "")
    {
    var someDate = new Date("December 31, 2023");
    var expiryDate = someDate.toGMTS tring();
    document.cookie = "0";

    cookie_counter = document.cookie ;
    document.write ("Cookie Created: " + cookie_counter) ;
    }
    else
    {
    var temp = parseInt(cookie _counter)
    temp = temp + 1
    cookie_counter = temp
    document.write ("Added 1 to Cookie: " + cookie_counter) ;
    }
    }

    But this just doesn't work. The first time through, cookie is created
    fine and I get a line that reads "Cookie Created: 0". But the second
    and following time I keep getting "Added 1 to Cookie: NaN".

    I can't figure out what's wrong. I don't even see any cookie file
    created in my cookies folder under windows. I even set my IE security
    levels to accept all cookies, but still nothing ...

    Any ideas?
  • kaeli

    #2
    Re: cookie example - where's the bug?

    In article <fdcee255.04042 70111.7e280499@ posting.google. com>,
    asadikhan@hotma il.com enlightened us with...[color=blue]
    > I am trying to get a cookie going on my site. Here is the code first:
    >
    > function setCookie()
    > {
    > if(document.coo kie == "")
    > {
    > var someDate = new Date("December 31, 2023");
    > var expiryDate = someDate.toGMTS tring();
    > document.cookie = "0";
    >
    > cookie_counter = document.cookie ;
    > document.write ("Cookie Created: " + cookie_counter) ;
    > }
    > else
    > {
    > var temp = parseInt(cookie _counter)[/color]

    Where is cookie_counter defined? The only place I see it is in that
    "if". Which means it has no value before this else because it's scope is
    local to the function.
    That is probably your problem, unless you defined it global somewhere
    else.
    Put an alert(cookie_co unter) just before this statement (the var temp
    one) and see what's in there.


    --
    --
    ~kaeli~
    Why do they sterilize the needles for lethal injections?



    Comment

    • Asad Khan

      #3
      Re: cookie example - where's the bug?

      No, that's not it. Yes I do declare it outside. Here is my full code
      once again:

      <script language="javas cript">
      <!--
      var cookie_counter

      function setCookie()
      {
      if (document.cooki e == "")
      {
      var someDate = new Date("December 31, 2023");
      var expiryDate = someDate.toGMTS tring();
      document.cookie ="0";

      cookie_counter = document.cookie ;
      document.write ("Cookie created!" + cookie_counter) ;
      }
      else
      {
      alert(cookie_co unter)
      document.write ("Cookie added!" + cookie_counter) ;
      var temp = parseInt(cookie _counter)
      temp = temp + 1
      cookie_counter = temp
      document.write ("Cookie added!" + temp+ "|" +
      cookie_counter) ;
      }
      }


      //-->

      </script>

      and setCookie is called on body onload. Same problem with alert. Says
      undefined!

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Stephen Chalmers

        #4
        Re: cookie example - where's the bug?


        "Asad Khan" <asadikhan@hotm ail.com> wrote in message
        news:408ebfc7$0 $202$75868355@n ews.frii.net...

        [color=blue]
        > and setCookie is called on body onload. Same problem with alert. Says
        > undefined![/color]

        Of course it does - if the block containing the alert() is executed,
        cookie_counter has been assigned nothing.

        The statement document.cookie =0 will save nothing.

        The minimum format for a cookie that survives the current session is;
        cookiename=valu e;expires=gmtst ring
        [color=blue]
        > if (document.cooki e == "")[/color]

        The document.cookie property is a string that can be comprised of many cookies,
        not all necessarily created by the current document. Consequently, to read the
        value of a particular cookie, you must search for its offset in the string.

        var cOffset, cName="myCookie =", cValue=null;

        if( document.cookie && (cOffset=docume nt.cookie.index Of( cName )) >-1 ) //get
        offset of cookie's name
        {
        cOffset+=cName. length; //bump to the start of the value

        cValue=document .cookie.substri ng(cOffset, document.cookie .length); //read from
        start of value

        if( !isNaN( cValue=parseInt ( cValue ) ) )
        cValue+=1 //or whatever you want to do with it
        }


        For debugging purposes, if you want to see the contents of document.cookie , type
        in the address bar:

        javascript:aler t(document.cook ie)

        --
        Stephen Chalmers




        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: cookie example - where's the bug?

          kaeli wrote:
          [color=blue]
          > In article <408ebfc7$0$202 $75868355@news. frii.net>,
          > asadikhan@hotma il.com enlightened us with...[color=green]
          >> No, that's not it. Yes I do declare it outside.[/color]
          >
          > But you don't assign it a value.[/color]

          True.
          [color=blue]
          > document.cookie has ALL the cookies. Not just yours.[/color]

          No, it has not. Otherwise it would be
          possible to read cookies of other sites.


          PointedEars

          Comment

          Working...