dot404
dot404

Reputation: 208

IE does not rember hidden field value on back button

Let's say I gave this code:

<input type="hidden" id="field" name="field" value="1">      
<button id="change">Change</button>

And this javascript:

alert($("#field").val());
$("#change").on("click", function(){
    var newValue = parseInt($("#field").val()) + 1;
   $("#field").val(newValue);
   alert($("#field").val());
});

If I change the value, go to a different page and then use the back button, in chrome and firefox the latest value is shown while in IE the default value. How to fix that without using sessionstorage?

Fiddle

Upvotes: 1

Views: 1686

Answers (3)

Francesc Josep Devesa
Francesc Josep Devesa

Reputation: 13

Please try to set value with:

$("#field").attr("value", newValue);

Don't use .val() method. I have a similar issue and I solved with it because IE not refresh a hidden value with .val().

Upvotes: 0

Seabizkit
Seabizkit

Reputation: 2415

you can not persist the value in the page itself..., any full page reload will clear any values which you have changed... so unless you save the state to some sort of persist layer... depending on the bowser the back button will behave in different ways... some much older browsers with no caching will reload the whole page.

Some may cache the actual javascript before the modifications and therefore load the old javascript and with the old value form the cache as well.

It is better to handle this in some sort of persist layer

see: Ajax, back button and DOM updates

Upvotes: 0

Cheezy Code
Cheezy Code

Reputation: 1715

This is not a standard practice, may be Firefox and Chrome are caching the values. But you should not rely on it.

If you refresh the page using Ctrl + F5, it will not work.

Upvotes: 2

Related Questions