Does back/forward in the browser change javascript variables?

放肆的年华 提交于 2019-11-30 15:27:40

It will be 0. The browser does not cache the state of Javascript variables between page loads.

Update

This is not the case in browsers such as Firefox. Please see Trey's answer.

Trey Hunner

As detailed in another question, the real answer to this question is it depends on the browser.

In Firefox and Opera, the below page will preserve the state of 1 if Set x is clicked, the link is clicked, and then the back button is pressed. However, in Chrome and IE6 the page will be reloaded and x will have the value of 0.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<input type="button" id="button" value="Set x">
<input type="button" id="check-x" value="Check x">
<a href="http://www.stackoverflow.com">Click Me</a>
<script>
var x = 0;

$("#button").click(function(){
    x = 1;
});

$("#check-x").click(function(){
   alert(x); 
});
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!