最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Does backforward in the browser change javascript variables? - Stack Overflow

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

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

</script>

Let's say the variable "x" changes to 1. Then the user clicks a link. When the user clicks "back", will x be 0 or 1?

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

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

</script>

Let's say the variable "x" changes to 1. Then the user clicks a link. When the user clicks "back", will x be 0 or 1?

Share Improve this question edited Sep 18, 2010 at 12:06 balupton 48.7k32 gold badges134 silver badges184 bronze badges asked Feb 24, 2010 at 2:53 TIMEXTIMEX 272k367 gold badges800 silver badges1.1k bronze badges 2
  • 2 You could test it, and let us know..? =) – David Thomas Commented Feb 24, 2010 at 3:04
  • hey original poster you may wanna choose the other answer as accepted :D – Sharky Commented Feb 13, 2015 at 11:56
Add a comment  | 

2 Answers 2

Reset to default 22

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>

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.

发布评论

评论列表(0)

  1. 暂无评论