javascript var re-initialization in IE and Firefox after back button.

G

gatejs

In IE. If you use the back button,
the resulting page will execute the javascript in the page again:

In Firefox, whatever value a variable had is kept when you go back to
it:

var i=0;
// after some interaction with this page, i=4
visit some link
press the back button.
IE: i=0
Firefox: i=4

IE will also execute a body onload event (Firefox will not).

Any way around this (I want the firefox behavior in IE.)
 
R

RobG

gatejs said on 26/04/2006 12:16 PM AEST:
In IE. If you use the back button,
the resulting page will execute the javascript in the page again:

In Firefox, whatever value a variable had is kept when you go back to
it:

var i=0;
// after some interaction with this page, i=4
visit some link
press the back button.
IE: i=0
Firefox: i=4

IE will also execute a body onload event (Firefox will not).

Any way around this (I want the firefox behavior in IE.)

Store the value somewhere other than a global variable. It seems to me
that IE is correct (or at least exhibits the expected behaviour), I
would expect the window object and all its properties to be destroyed
when the user navigates away from the page.

Anyhow, the following example stores the value in a hidden input, you
may want to use a cookie depending on whether all browsers retain form
values when navigating back to the page or not (I haven't tested too
widely):


<script type="text/javascript">

function addOne(){
var el = document.getElementById('store_x');
el.value = +el.value + 1;
return el.value;
}

</script>

<button onclick="alert('x is now: ' + addOne());">Add one</button>
<a href="http://www-internal.qdot.qld.gov.au/">Home</a>
<input type="hidden" id="store_x" value="0">
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top