Can't write cookie in IE

Discussion in 'Javascript' started by Aki, May 19, 2005.

  1. Aki

    Aki Guest

    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=JavaScript>
    function alertti(){
    if (document.cookie !== "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
    Aki, May 19, 2005
    #1
    1. Advertising

  2. Aki

    VK Guest

    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.
    VK, May 19, 2005
    #2
    1. Advertising

  3. Aki <> wrote in message
    news:...


    > if (document.cookie !== "kysely=1") file://never another cookie


    document.cookie is/can be comprised of other strings as well as the one you
    seek, so search for it with document.cookie.indexOf("kysely=1"), having
    checked previously that document.cookie exists.

    > document.cookie = "kysely=1; expires=01-Jan-06";


    The expiry date has to be in the correct format as generated by
    Date.toGMTString() .
    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
    547265617375726520627572696564206174204F2E532E207265663A205451323437393134
    Stephen Chalmers, May 19, 2005
    #3
  4. Aki

    Aki Guest

    Thank you Stephen! By giving the expiry date as you suggested:

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


    instead of the previous way:

    > > document.cookie = "kysely=1; expires=01-Jan-06";


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

    Regards,
    Aki
    Aki, May 19, 2005
    #4
  5. JRS: In article <>, dated Thu,
    19 May 2005 13:44:17, seen in news:comp.lang.javascript, Stephen
    Chalmers <> posted :


    >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,


    That cannot be known reliably and accurately in general.

    > 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();


    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.getDate()+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.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr John Stockton, May 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page