Cookie question

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

    Cookie question

    I learning JavaScript and doing something wrong here, what I want to
    do is store the contents of a variable to a cookie so that it would
    show the next time the page was opened. I'm doing something wrong in
    the code below because instead of saving the value of nLastBonus to
    the cookie I'm saving "nLastBonus ".

    Can someone point me in the right direction?

    // Get the cookie
    var nLastBonus = get_cookie()

    // Did the cookie exist?
    if (nLastBonus) {

    // If not, then store 100 to nLastBonus
    // visit_number = 1
    nLastBonus = 100
    }
    else {

    // Otherwise, increment nLastBonus
    nLastBonus++
    }

    // Set the cookie
    document.cookie ="last_bonus=nL astBonus; expires=" + expire_string
  • Lasse Reichstein Nielsen

    #2
    Re: Cookie question

    raccoon@icubed. com (Jim Davidson) writes:
    [color=blue]
    > I'm doing something wrong in the code below because instead of
    > saving the value of nLastBonus to the cookie I'm saving
    > "nLastBonus ".[/color]

    I assume the rest works. Change:
    [color=blue]
    > document.cookie ="last_bonus=nL astBonus; expires=" + expire_string[/color]
    to
    document.cookie ="last_bonus="+ escape(nLastBon us)"+; expires="+expir e_string;

    Generally, you should escape the values stored in cookies to make sure
    they don't contain meaningfull characters. In this case, it isn't
    necessary, since numbers are safe, but if nLastBonus was the string
    "foo;expires=no w;" (or something similar that works) then you will get
    trouble.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Janwillem Borleffs

      #3
      Re: Cookie question


      "Lasse Reichstein Nielsen" <lrn@hotpop.com > schreef in bericht
      news:vfqn8xmu.f sf@hotpop.com.. .[color=blue]
      >
      > document.cookie ="last_bonus="+ escape(nLastBon us)"+;[/color]
      expires="+expir e_string;[color=blue]
      >[/color]

      And when you get an error on the above line, change it into:

      document.cookie ="last_bonus="+ escape(nLastBon us)+"; expires="+expir e_string;


      JW


      Comment

      • Jim Davidson

        #4
        Re: Cookie question

        "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message news:<3f90481f$ 0$2731$1b62eedf @news.euronet.n l>...[color=blue]
        > "Lasse Reichstein Nielsen" <lrn@hotpop.com > schreef in bericht
        > news:vfqn8xmu.f sf@hotpop.com.. .[color=green]
        > >
        > > document.cookie ="last_bonus="+ escape(nLastBon us)"+;[/color]
        > expires="+expir e_string;[color=green]
        > >[/color]
        >
        > And when you get an error on the above line, change it into:
        >
        > document.cookie ="last_bonus="+ escape(nLastBon us)+"; expires="+expir e_string;
        >
        >
        > JW[/color]


        Thanks guys that worked!

        Comment

        Working...