By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Cookie question

P: n/a
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=nLastBonus; expires=" + expire_string
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ra*****@icubed.com (Jim Davidson) writes:
I'm doing something wrong in the code below because instead of
saving the value of nLastBonus to the cookie I'm saving
"nLastBonus".
I assume the rest works. Change:
document.cookie="last_bonus=nLastBonus; expires=" + expire_string

to
document.cookie="last_bonus="+escape(nLastBonus)"+ ; expires="+expire_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=now;" (or something similar that works) then you will get
trouble.

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

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:vf**********@hotpop.com...

document.cookie="last_bonus="+escape(nLastBonus)"+ ; expires="+expire_string;


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

document.cookie="last_bonus="+escape(nLastBonus)+" ; expires="+expire_string;
JW
Jul 20 '05 #3

P: n/a
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<3f**********************@news.euronet.nl>...
"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:vf**********@hotpop.com...

document.cookie="last_bonus="+escape(nLastBonus)"+ ;

expires="+expire_string;


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

document.cookie="last_bonus="+escape(nLastBonus)+" ; expires="+expire_string;
JW

Thanks guys that worked!
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.