468,268 Members | 1,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

document.cookie always returns empty string

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.
Jan 15 '07 #1
6 19035
VK
Michi Henning wrote:
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.

Jan 15 '07 #2
dd
Michi Henning wrote:
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.

Jan 16 '07 #3
dd
My forward slash in the path= field got parsed out by google groups
somehow :(

Jan 16 '07 #4
In comp.lang.javascript message <11**********************@11g2000cwr.goog
legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd****@gmail.composted:
>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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 17 '07 #5
Dr J R Stockton said the following on 1/17/2007 9:22 AM:
In comp.lang.javascript message <11**********************@11g2000cwr.goog
legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd****@gmail.composted:
>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.
Too bad nothing you wrote had anything to do with the problem or any
potential solution to the problem.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 17 '07 #6
In comp.lang.javascript message <b4********************@telcove.net>,
Wed, 17 Jan 2007 17:26:18, Randy Webb <Hi************@aol.composted:
>Dr J R Stockton said the following on 1/17/2007 9:22 AM:
>In comp.lang.javascript message <11**********************@11g2000cwr.goog
legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd****@gmail.composted:
>>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.

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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ralph Freshour | last post: by
4 posts views Thread by Geoff May | last post: by
6 posts views Thread by David List | last post: by
12 posts views Thread by Kepler | last post: by
136 posts views Thread by Matt Kruse | last post: by
17 posts views Thread by rox.scott | last post: by
10 posts views Thread by icanhelp33 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.