473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 20790
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ralph Freshour | last post by:
I wrote a small .php script that creates a cookie on my local PC - I also wrote one that deletes it. My question is this: when the cookie has been created and I run the delete .php script, when...
4
by: Geoff May | last post by:
I'm busy rewriting by F1 database and I want to use cookies to store various user definable views, (basically so that when the visitor returns to a specific page, that page will redisplay in the...
4
by: Yvan J. Gagnon | last post by:
I am encountering a strange problem in Netscape 7 with a CFM file I am trying to troubleshoot (the page is working fine in NS Communicator and IE). Below is a sample of the problematic line of...
6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
10
by: InvisibleMan | last post by:
Hi, Thanks for any help in advance... Okay, I have the JS listed below that calls for the display of the (DIV) tag... cookie function not included, as don't feel its necessary but you'll get the...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
17
by: rox.scott | last post by:
Can someone please explain why this happens? The expected output is 3, but uncommenting line 7 makes the output 0. Why ??? VB.NET code: ** note the commented line, this is the culprit ** Dim...
10
by: icanhelp33 | last post by:
I have a C# utility that runs in ASP website. When user logs in the ASP code writes the cookie using Response.Cookie. I try to read the same cookie in C#. I am unable to read it in C# code. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.