document.cookie always returns empty string

M

Michi Henning

I'm running the following code in Safari 2.0.4:

document.cookie = 'MyCookie=1';
if(document.cookie == '')
alert('document.cookie is empty!');

document.cookie always returns the empty string, no matter what I do.
I checked the cookie settings in Safari->Preferences->Security. Even
when I set cookies to be always accepted, document.cookie still returns
the empty string.

Needless to say, the same code works just fine with a various other
browsers.

I'd be most grateful for any hints as to what I'm doing wrong.

Thanks,

Michi.
 
V

VK

Michi said:
I'm running the following code in Safari 2.0.4:

document.cookie = 'MyCookie=1';
if(document.cookie == '')
alert('document.cookie is empty!');

document.cookie always returns the empty string, no matter what I do.

1) Safari 1.x-2.0.4 is an ugly junk.
2) Safari 1.x-2.0.4 is useless crap.
3) For MAC OS use Camino, Opera or Firefox (listed in order of
preference)

....what was I about? ...oh, yeh - Safari only stores cookies if set
over an HTTP-connection. So for a file open over the file system it
won't work.
 
D

dd

Michi said:
I'm running the following code in Safari 2.0.4:

document.cookie = 'MyCookie=1';
if(document.cookie == '')
alert('document.cookie is empty!');

That's only creating a session cookie. If you want it to persist (be
stored in a file) then you need to add an expiry date (in the future)
to it:

var exp=new Date();
var numdays=7;
exp.setTime(exp.getTime()+(1000*60*60*24*numdays));
document.cookie="MyCookie=1; path=; expires="+exp.toGMTString();

It's always best to have the test page hosted on a server when you're
testing cookie stuff. I wouldn't even try using a local server (on
localhost) when it comes to cookie code.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
var exp=new Date();
var numdays=7;
exp.setTime(exp.getTime()+(1000*60*60*24*numdays));
document.cookie="MyCookie=1; path=; expires="+exp.toGMTString();


That's not a good way to increment a date.

Firstly, you could use 864e5 instead of 1000*60*60*24
Secondly, the innermost parentheses are unnecessary.
Thirdly, in many countries for about 4% of the time that will not give 7
civil days. Often the exact expiry does not matter, but there is a
possibility of the user coming to expect it.

exp.setDate(exp.getDate()+numdays);

This is actually a case where "with" can be used in safety :

with (new Date()) {
var numdays = 7
setDate(getDate() + numdays)
document.cookie="MyCookie=1; path=; expires=" + toGMTString() }

It's a good idea to read the newsgroup and its FAQ. See below.
 
R

Randy Webb

Dr J R Stockton said the following on 1/17/2007 9:22 AM:
In comp.lang.javascript message <[email protected]



That's not a good way to increment a date.

Too bad nothing you wrote had anything to do with the problem or any
potential solution to the problem.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 1/17/2007 9:22 AM:

Too bad nothing you wrote had anything to do with the problem or any
potential solution to the problem.


Too bad that you did not comprehend the desirability of pointing out
defects in a proposed solution.

It's a good idea to read the newsgroup and its FAQ. See below.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top