473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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=nLastBonus; expires=" + expire_string
Jul 20 '05 #1
3 1727
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ralph Freshour | last post by:
I wrote a small .php script that creates a cookie on my local PC - I also wrote one that deletes it. My question is this: when the cookie has been created and I run the delete .php script, when...
6
by: Ajay | last post by:
hi! i am printing a simple cookie, but instead of printing um=name:blah&access:admin&exp:2312390.909 its printing um="name:blah&access:admin&exp:2312390.909" why the quotes?
1
by: koen colen | last post by:
Hello group, I hope you guys can help me out, I am modifying a piece of code from Joe Norman, I did found the code here: http://www.intranet2internet.com/public/default.asp?PAGE=sscript&ID=3 ...
1
by: Display Name | last post by:
Used one of these canned scripts to set up a JS quiz but not before having used another canned PHP script for "Tell your friend about this Web page!" sort of thing. Now i've gotta integrate them;...
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
7
by: odonel | last post by:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: here is my code: HttpCookie cookie = Request.Cookies;
15
by: Oleg Leikin | last post by:
Hi, (newbie question) I've created some simple .NET ASP application that should store cookies at the client machine. According to the documentation cookie expiration time is set via...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
3
by: Dan | last post by:
Hi, I am trying to refresh the cookie to make sure the timeout is reset by simply calling a blank page on my site. I am doing this because I have an external site hosted in my web that isn't...
16
by: Stevo | last post by:
I'm guessing this is a laughably obvious answer to many here, but it's not to me (and I don't have a server or any knowledge of PHP to be able to try it). It's not strictly a PHP question, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.