Web Hosting Talk







View Full Version : javascript cookies (newbie question)


horoscopes2000
03-16-2002, 12:38 AM
Hi Guys,

I have just today started out with Javascript and can't seem to get this
snippet of code below to write the contents of "dat"
(testdatatestdatatestdatatestdatatestdatatestdata) to the cookie. Once I
have that working I can start trying it with some "real" data.

The following code is in the <body> of my test page.

****
<script language="javascript">
document.cookie="cookietest"
var exp = new Date()
var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000)
exp.setTime(oneYearFromNow);
var dat = "testdatatestdatatestdatatestdatatestdatatestdata"
document.cookie = "exp; expires=" + exp.toGMTString();
</script>
****

When I run this locally, my cookie gathers the following info :

exp~~local~~/C:\WINDOWS\Temporary Internet
Files\Content.IE5\6HTYRUP4\1088135140569629551474362368179229478048*

Any ideas?

jks
03-16-2002, 10:22 AM
Originally posted by horoscopes2000

document.cookie = "exp; expires=" + exp.toGMTString();
</script>


Try changing this to:

document.cookie = "cookieTest=" + dat + "; expires=" + exp.toGMTString();

horoscopes2000
03-16-2002, 10:51 AM
Originally posted by jks


Try changing this to:

document.cookie = "cookieTest=" + dat + "; expires=" + exp.toGMTString();

aah, that's great, thank you.

Now on to the next stage of figuring it all out. :o)

Part of my problem was also that I was trying to make it work locally, which I have just discovered is not possible.

horoscopes2000
03-16-2002, 04:02 PM
Hmm, I've just thought.

How can I make this only write to the cookie if it doesn't already exist? I only want it to write once.