Can't write cookie in IE

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

    #1

    Can't write cookie in IE

    Hi,

    I have a problem with writing cookies in IE. The code works fine in
    Netscape 7.2, Opera 8 and Firefox 1.04. In IE 6 the cookie isn't
    written. I run WinXP SP2.

    I'm trying to show a JavaScript alert reminding visitors about a
    questionnaire on the site. I launch the alert in an onLoad-event, but
    since I don't want to punish the visitors by showing the alert every
    time they visit the page, I write a cookie when the alert-window is
    closed. Before the alert is launched, I check if the cookie exists,
    and the alert will be shown only if the cookie isn't found. This
    should be rather simple.

    Has anyone got an idea how to get this working?

    I'm using the following code:

    <html>

    <head>


    <script language=JavaSc ript>
    function alertti(){
    if (document.cooki e !== "kysely=1") //never another cookie
    alert("Have you noticed our questionnaire?" );
    document.cookie = "kysely=1; expires=01-Jan-06";
    }
    </script>

    </head>

    <body onLoad=alertti( )>


    </body>
    </html>

    Regards,

    Aki
  • VK

    #2
    Re: Can't write cookie in IE

    Try instead:
    document.cookie = "kysely=1";
    (no expiration part). This creates a session cookie - exists only
    while browser window is not closed.

    Persistent cookies in SP2 (and further) only allowed if:
    1) You have proper P3P declarations on your site (google for "Platform
    for Privacy Preferences")
    2) These declarations are matching the current user's cookie policies.

    Unless of course someone changed the default settings, but you cannot
    count on it.

    Comment

    • Stephen Chalmers

      #3
      Re: Can't write cookie in IE

      Aki <aki.ojala@blue bottle.com> wrote in message
      news:e81ef747.0 505190101.6201b dbe@posting.goo gle.com...

      [color=blue]
      > if (document.cooki e !== "kysely=1") file://never another cookie[/color]

      document.cookie is/can be comprised of other strings as well as the one you
      seek, so search for it with document.cookie .indexOf("kysel y=1"), having
      checked previously that document.cookie exists.
      [color=blue]
      > document.cookie = "kysely=1; expires=01-Jan-06";[/color]

      The expiry date has to be in the correct format as generated by
      Date.toGMTStrin g() .
      To determine the expiry date, read the current time in milliseconds with
      Date.getTime(), add to it the total number of milliseconds in the number of
      days duration you require, then pass that value to the Date() constructor to
      generate a new object equating to your desired expiry date. Call
      ..toGMTString for the new object, and append its return value to the
      'expires' parameter when writing the cookie.

      So for a 30-day cookie you could write:

      document.cookie = "kysely=1; expires=" + new Date( new
      Date().getTime( )+86400000*30 ).toGMTString() ;

      --
      Stephen Chalmers
      547265617375726 520627572696564 206174204F2E532 E207265663A2054 51323437393134



      Comment

      • Aki

        #4
        Re: Can't write cookie in IE

        Thank you Stephen! By giving the expiry date as you suggested:
        [color=blue]
        > document.cookie = "kysely=1; expires=" + new Date( new
        > Date().getTime( )+86400000*30 ).toGMTString() ;[/color]

        instead of the previous way:
        [color=blue][color=green]
        > > document.cookie = "kysely=1; expires=01-Jan-06";[/color][/color]

        helped IE to understand what I wanted. Now the code is running
        smoothly in all browsers I've tested it with.

        Regards,
        Aki

        Comment

        • Dr John Stockton

          #5
          Re: Can't write cookie in IE

          JRS: In article <428c89ef_4@m k-nntp-2.news.uk.tisca li.com>, dated Thu,
          19 May 2005 13:44:17, seen in news:comp.lang. javascript, Stephen
          Chalmers <ignoring@lycos .co.uk> posted :

          [color=blue]
          >To determine the expiry date, read the current time in milliseconds with
          >Date.getTime() , add to it the total number of milliseconds in the number of
          >days duration you require,[/color]

          That cannot be known reliably and accurately in general.
          [color=blue]
          > then pass that value to the Date() constructor to
          >generate a new object equating to your desired expiry date. Call
          >.toGMTString for the new object, and append its return value to the
          >'expires' parameter when writing the cookie.
          >
          >So for a 30-day cookie you could write:
          >
          >document.cooki e = "kysely=1; expires=" + new Date( new
          >Date().getTime ()+86400000*30 ).toGMTString() ;[/color]

          It will overrun an hour of civil time in Spring, and underrun an hour in
          Autumn, in many locations.

          D = new Date() ; D.setDate(D.get Date()+30)
          document.cookie = "kysely=1; expires=" + D.toGMTString() ;
          or
          with (new Date()) { setDate(getDate ()+30)
          document.cookie = "kysely=1; expires=" + toGMTString() }

          When using 86400000 I prefer to write 864e5; shorter, and no risk of
          miscounting zeroes.

          Code containing that number disregards Summer Time, more often than not.

          Of course, for many purposes an hour in a month does not matter; but the
          code might be copied with a shorter interval, and an unwary programmer
          might not consider the difference between "this expires in 24 hours" and
          "this expires at this time tomorrow.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
          Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
          PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
          Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

          Comment

          Working...