cookies, onLoad and IE v Firefox

C

Clive Backham

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top