Reputation: 208
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?
Upvotes: 1
Views: 1686
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
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
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