cookies, onLoad and IE v Firefox

Discussion in 'Javascript' started by Clive Backham, Apr 9, 2008.

  1. For reasons I won't bore people with, I'm modifying a session cookie
    inside an event handler and then analysing the cookie in the onLoad
    page handler when the user returns to the same page having navigated
    away from it in the meantime. My problem is that the behaviour is
    different in IE and Firefox.

    I've tried to pare this down to the simpest page that demonstrates the
    issue:

    <html>
    <head>
    <script type="text/javascript">
    function init()
    {
    showcookie();
    }
    function showcookie()
    {
    document.getElementById("log").value = document.cookie;
    }
    function modifycookie()
    {
    document.cookie = "timestamp=" + new Date().toString();
    }
    </script>
    </head>
    <body onload="init();">
    <input name=log id=log size=100><br>
    <input type=button value="show cookie" onclick="showcookie();"><br>
    <input type=button value="modify cookie" onclick="modifycookie();">
    </body>
    </html>

    Here's the sequence of events that show the problem:
    1. Load the page.
    2. Click the "modify cookie" button.
    3. Hit the Back button, then the Forward button.
    4. Note the value of the "log" field.
    5. Click the "show cookie" button and note again the value of the
    "log" field.

    I would expect the values noted in steps 4 and 5 to be the same, and
    in IE they are. But in Firefox (and Opera) they are not.

    If we add this extra action between steps 2 and 3:
    2A. Click the "show cookie" button.

    Then Firefox and Opera behave as expected: after a Back and Forward,
    the "log" field shows the updated value.

    It's almost as if Firefox and Opera are not setting the value of
    document.cookie until after the onLoad handler has completed.

    I must be missing something fundamental here.
     
    Clive Backham, Apr 9, 2008
    #1
    1. Advertising

  2. Clive Backham

    Ed Guest

    On Apr 9, 6:47 am, Clive Backham <> wrote:
    > 3. Hit the Back button, then the Forward button.


    The onload handler is probably not getting called here in Firefox/
    Opera.

    > It's almost as if Firefox and Opera are not setting the value of
    > document.cookie until after the onLoad handler has completed.


    The value is from the back-forward cache. See:

    http://developer.mozilla.org/en/docs/Using_Firefox_1.5_caching
     
    Ed, Apr 10, 2008
    #2
    1. Advertising

  3. On 10 Apr, 05:45, Ed <> wrote:
    > On Apr 9, 6:47 am, Clive Backham <> wrote:
    >
    > > 3. Hit the Back button, then the Forward button.

    >
    > The onload handler is probably not getting called here in Firefox/
    > Opera.


    So is that "correct" in a formal sense? Should browsers be allowed to
    just ignore the onLoad event?

    > The value is from the back-forward cache.  See:
    >
    >  http://developer.mozilla.org/en/docs/Using_Firefox_1.5_caching


    Thanks for the link. I'll experiment with the onPageShow event - it
    looks like it should do what I need.
     
    Clive Backham, Apr 10, 2008
    #3
    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.
Similar Threads
  1. Fabio R.
    Replies:
    0
    Views:
    800
    Fabio R.
    Mar 25, 2005
  2. _Who
    Replies:
    7
    Views:
    2,676
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    557
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    5
    Views:
    262
    Thomas 'PointedEars' Lahn
    May 15, 2005
  5. marco

    onload -->* no onload

    marco, Jun 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    206
    marco
    Jun 24, 2006
Loading...

Share This Page