views:

57

answers:

2
<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?

A: 

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

Justin Ethier
That's weird, I must have done something wrong. Was my code below wrong? I don't do much javascript, just trying to see why I get different behavior.
Anthony Forloney
But sometimes clicking a link then clicking back won't result in a page load, as when the target of the link is a local anchor.
Sean
A: 

All variables are "forgotten" by the browser when you navigate to a different page... With the exception of localStorage and its sister apis...

You are not using localStorage, and therefore, the variable will have a value of 0

ItzWarty