Can't write cookie in IE

A

Aki

Hi,

I have a problem with writing cookies in IE. The code works fine in
Netscape 7.2, Opera 8 and Firefox 1.04. In IE 6 the cookie isn't
written. I run WinXP SP2.

I'm trying to show a JavaScript alert reminding visitors about a
questionnaire on the site. I launch the alert in an onLoad-event, but
since I don't want to punish the visitors by showing the alert every
time they visit the page, I write a cookie when the alert-window is
closed. Before the alert is launched, I check if the cookie exists,
and the alert will be shown only if the cookie isn't found. This
should be rather simple.

Has anyone got an idea how to get this working?

I'm using the following code:

<html>

<head>


<script language=JavaScript>
function alertti(){
if (document.cookie !== "kysely=1") //never another cookie
alert("Have you noticed our questionnaire?");
document.cookie = "kysely=1; expires=01-Jan-06";
}
</script>

</head>

<body onLoad=alertti()>


</body>
</html>

Regards,

Aki
 
V

VK

Try instead:
document.cookie = "kysely=1";
(no expiration part). This creates a session cookie - exists only
while browser window is not closed.

Persistent cookies in SP2 (and further) only allowed if:
1) You have proper P3P declarations on your site (google for "Platform
for Privacy Preferences")
2) These declarations are matching the current user's cookie policies.

Unless of course someone changed the default settings, but you cannot
count on it.
 
S

Stephen Chalmers

if (document.cookie !== "kysely=1") file://never another cookie

document.cookie is/can be comprised of other strings as well as the one you
seek, so search for it with document.cookie.indexOf("kysely=1"), having
checked previously that document.cookie exists.
document.cookie = "kysely=1; expires=01-Jan-06";

The expiry date has to be in the correct format as generated by
Date.toGMTString() .
To determine the expiry date, read the current time in milliseconds with
Date.getTime(), add to it the total number of milliseconds in the number of
days duration you require, then pass that value to the Date() constructor to
generate a new object equating to your desired expiry date. Call
..toGMTString for the new object, and append its return value to the
'expires' parameter when writing the cookie.

So for a 30-day cookie you could write:

document.cookie = "kysely=1; expires=" + new Date( new
Date().getTime()+86400000*30 ).toGMTString();
 
A

Aki

Thank you Stephen! By giving the expiry date as you suggested:
document.cookie = "kysely=1; expires=" + new Date( new
Date().getTime()+86400000*30 ).toGMTString();

instead of the previous way:

helped IE to understand what I wanted. Now the code is running
smoothly in all browsers I've tested it with.

Regards,
Aki
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu,
19 May 2005 13:44:17, seen in Stephen
To determine the expiry date, read the current time in milliseconds with
Date.getTime(), add to it the total number of milliseconds in the number of
days duration you require,

That cannot be known reliably and accurately in general.
then pass that value to the Date() constructor to
generate a new object equating to your desired expiry date. Call
.toGMTString for the new object, and append its return value to the
'expires' parameter when writing the cookie.

So for a 30-day cookie you could write:

document.cookie = "kysely=1; expires=" + new Date( new
Date().getTime()+86400000*30 ).toGMTString();

It will overrun an hour of civil time in Spring, and underrun an hour in
Autumn, in many locations.

D = new Date() ; D.setDate(D.getDate()+30)
document.cookie = "kysely=1; expires=" + D.toGMTString();
or
with (new Date()) { setDate(getDate()+30)
document.cookie = "kysely=1; expires=" + toGMTString() }

When using 86400000 I prefer to write 864e5; shorter, and no risk of
miscounting zeroes.

Code containing that number disregards Summer Time, more often than not.

Of course, for many purposes an hour in a month does not matter; but the
code might be copied with a shorter interval, and an unwary programmer
might not consider the difference between "this expires in 24 hours" and
"this expires at this time tomorrow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top