Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can't write cookie in IE

Reply
Thread Tools

Can't write cookie in IE

 
 
Aki
Guest
Posts: n/a
 
      05-19-2005
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
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      05-19-2005
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.

 
Reply With Quote
 
 
 
 
Stephen Chalmers
Guest
Posts: n/a
 
      05-19-2005
Aki <> wrote in message
news: om...


> 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();

--
Stephen Chalmers
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134



 
Reply With Quote
 
Aki
Guest
Posts: n/a
 
      05-19-2005
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:

> > document.cookie = "kysely=1; expires=01-Jan-06";


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

Regards,
Aki
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      05-19-2005
JRS: In article <>, dated Thu,
19 May 2005 13:44:17, seen in news:comp.lang.javascript, Stephen
Chalmers <> posted :


>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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is different between Request.Cookie and Response.Cookie ad ASP .Net 2 01-27-2006 12:54 PM
Cookie Question (IP as domain and cookie file location) =?Utf-8?B?UGF1bA==?= ASP .Net 1 01-10-2006 08:37 PM
Any downsides to cookie assignment inside custom class using HttpContext.Current? ASP.NET 2.0 cookie fix? ASP .Net 2 08-17-2005 06:43 AM
Cookie and Session Cookie Questions. Shapper ASP .Net 1 04-27-2005 11:20 AM
Session cookie? Browser instance cookie? Ben ASP .Net 3 06-03-2004 03:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57