473,788 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.cookie always returns empty string

I'm running the following code in Safari 2.0.4:

document.cookie = 'MyCookie=1';
if(document.coo kie == '')
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 21028
VK
Michi Henning wrote:
I'm running the following code in Safari 2.0.4:

document.cookie = 'MyCookie=1';
if(document.coo kie == '')
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.coo kie == '')
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()+(100 0*60*60*24*numd ays));
document.cookie ="MyCookie=1 ; path=; expires="+exp.t oGMTString();

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.javas cript message <11************ **********@11g2 000cwr.goog
legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd****@gmail.c omposted:
>var exp=new Date();
var numdays=7;
exp.setTime(ex p.getTime()+(10 00*60*60*24*num days));
document.cooki e="MyCookie=1 ; path=; expires="+exp.t oGMTString();

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()+numd ays);

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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.javas cript message <11************ **********@11g2 000cwr.goog
legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd****@gmail.c omposted:
>var exp=new Date();
var numdays=7;
exp.setTime(ex p.getTime()+(10 00*60*60*24*num days));
document.cooki e="MyCookie=1 ; path=; expires="+exp.t oGMTString();


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.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 17 '07 #6
In comp.lang.javas cript message <b4************ ********@telcov e.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.javas cript message <11************ **********@11g2 000cwr.goog
legroups.com >, Tue, 16 Jan 2007 00:18:52, dd <dd****@gmail.c omposted:
>>var exp=new Date();
var numdays=7;
exp.setTime(e xp.getTime()+(1 000*60*60*24*nu mdays));
document.cook ie="MyCookie=1 ; path=; expires="+exp.t oGMTString();
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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
3357
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 it first loads it does not delete the cookie - I always have to click on the browser refresh button - why doesn't the script execute on the first execution? Thanks...
4
2644
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 format that visitor saved). Problem is, I don't know if the cookie was accepted or not. setcook() returns TRUE if it worked but that doesn't indicate whether or not it was accepted. I tried the following:
4
2766
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 javascript code. Note that in the actual file, I've surrounded the variable '#cookie.cookiename#' with CFOUTPUT tags: document.write('<input type="Hidden" name="affiliate" value="#cookie.cookiename#">'); When I load the document in Netscape 7, I...
6
6846
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 these problems is using document.layers. I have Google'd for examples of how to use the document object specifically with Mozilla, but I cannot find anything that explains why my problems occur. Could anyone here see through the included example...
12
10176
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 scrollbar, you get the height of the entire document, screwing up any chance of centering a window in the browser using these values. Is there a way to get the height of the actual browser window and not the entire page height? Thanks.
10
54906
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 idea! function closeall() { var objs;
136
9459
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
17
4617
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 xsl As New System.Xml.Xsl.XslTransform() Dim xw As New System.IO.StringWriter() Dim xmldoc As New System.Xml.XmlDocument() Dim xsldoc As New System.Xml.XmlDocument() xmldoc.Load("test.xml")
10
1697
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 using Request.Cookies.Value.ToString(). I can read the same cookie in ASP code. If I say Request.Cookies("CookieName") in ASP that works fine..
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.