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

Discussion in 'Javascript' started by gatejs, Apr 26, 2006.

  1. gatejs

    gatejs Guest

    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.)
     
    gatejs, Apr 26, 2006
    #1
    1. Advertising

  2. gatejs

    RobG Guest

    Re: javascript var re-initialization in IE and Firefox after backbutton.

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



    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/FAQ>
     
    RobG, Apr 26, 2006
    #2
    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. sylvia sil
    Replies:
    1
    Views:
    625
    Curt_C [MVP]
    Dec 29, 2004
  2. Replies:
    1
    Views:
    2,139
  3. Author
    Replies:
    6
    Views:
    3,104
    George
    Jan 22, 2009
  4. Replies:
    0
    Views:
    302
  5. Useful Info

    Firefox back button takes me back 2 pages.

    Useful Info, Jun 3, 2007, in forum: Javascript
    Replies:
    0
    Views:
    347
    Useful Info
    Jun 3, 2007
Loading...

Share This Page