473,405 Members | 2,344 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,405 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 1728
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.