浏览器中的后退/前进会改变 javascript 变量吗?

2023-11-21

<script type="text/javascript>
var x = 0; //this occurs in the beginning of the page.

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

</script>

假设变量“x”更改为 1。然后用户单击一个链接。当用户点击“后退”时,x会是0还是1?


详见另一个问题,这个问题的真正答案是这取决于浏览器.

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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

浏览器中的后退/前进会改变 javascript 变量吗? 的相关文章

随机推荐