document.cookie always returns empty string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michi Henning

    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.
  • VK

    #2
    Re: document.cookie always returns empty string

    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.

    Comment

    • dd

      #3
      Re: document.cookie always returns empty string

      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.

      Comment

      • dd

        #4
        Re: document.cookie always returns empty string

        My forward slash in the path= field got parsed out by google groups
        somehow :(

        Comment

        • Dr J R Stockton

          #5
          Re: document.cookie always returns empty string

          In comp.lang.javas cript message <1168935532.008 097.108620@11g2 000cwr.goog
          legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd4005@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.

          Comment

          • Randy Webb

            #6
            Re: document.cookie always returns empty string

            Dr J R Stockton said the following on 1/17/2007 9:22 AM:
            In comp.lang.javas cript message <1168935532.008 097.108620@11g2 000cwr.goog
            legroups.com>, Tue, 16 Jan 2007 00:18:52, dd <dd4005@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/

            Comment

            • Dr J R Stockton

              #7
              Re: document.cookie always returns empty string

              In comp.lang.javas cript message <b4idnRPaYZNbPD PY4p2dnA@telcov e.net>,
              Wed, 17 Jan 2007 17:26:18, Randy Webb <HikksNotAtHome @aol.composted:
              >Dr J R Stockton said the following on 1/17/2007 9:22 AM:
              >In comp.lang.javas cript message <1168935532.008 097.108620@11g2 000cwr.goog
              >legroups.com >, Tue, 16 Jan 2007 00:18:52, dd <dd4005@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.

              Comment

              Working...