Can't set session cookie???

B

Brett

I'd like to set a session cookie that expires once the browser is
closed. I've tried:

document.cookie = "wm_javascript=" + escape("SomeName|" +
window.document.referrer);

with no expires. This doesn't set a cookie. It seems I must put in
an expiration for the cookie to set. The code below does work:

var the_name = window.document.referrer;
var the_cookie = "wm_javascript=" + escape("SomeName|" + the_name);
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_date.toGMTString();
document.cookie = the_cookie;

Why doesn't the session cookie set? I'm using IE6.

Also, if a user has multiple IE windows open, will the session cookie
expires only after all IE windows have closed?

Thanks,
Brett
 
T

Thomas 'PointedEars' Lahn

Brett said:
document.cookie = "wm_javascript=" + escape("SomeName|" +
window.document.referrer);
with no expires. This doesn't set a cookie. It seems I must put in
an expiration for the cookie to set. The code below does work:

var the_name = window.document.referrer;

document.referrer is unreliable and using it is error-prone.
var the_cookie = "wm_javascript=" + escape("SomeName|" + the_name);
var the_date = new Date("December 31, 2050");

Using date strings is error-prone because interpretation is
implementation-dependent. Use several arguments instead:

var the_date = new Date(2050, 11, 31);
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_date.toGMTString();
document.cookie = the_cookie;

Why doesn't the session cookie set? I'm using IE6.

Implementations usually follow the Netscape cookie specification:

<http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/cookies.html#1002170>

Some UAs set cookies only if set by a resource retrieved via HTTP, thus
they do not set it with documents accessed with "file:" URIs, for example.
Some UAs consider cookies without "domain=" invalid and will not set those.
Also, if a user has multiple IE windows open, will the session cookie
expires only after all IE windows have closed?

Possibly.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 28 Jul
2004 05:00:01, seen in Thomas 'PointedEars'
Lahn said:
Using date strings is error-prone because interpretation is
implementation-dependent. Use several arguments instead:

var the_date = new Date(2050, 11, 31);

Which browsers do not understand new Date("December 31, 2050"); ?

It is non-ISO numeric date strings that are unreliable; but "2050/12/31"
should be safe everywhere, and is understandable by anybody; whereas
some people think that December needs a Zed, or possibly a Zee, or
transpose letters in it.

In this case, "01/01/2051" could have been used.
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top