Maybe I am missing something obvious, but how can I rewrite this code so that it does not need the variable to be a global variable?
<script language="javascript">
theVariable = "";
function setValue() /* called on page load */
{
theVariable = "a string of json data waiting to be eval()'d";
}
function getValue()
{
alert(theVariable);
}
</script>
<input type="button" onClick="javascript:getValue()" value="Get the value">
In my real situation, the setValue function makes an ajax call to the server, receives a json string and the data accessed when you hover over different parts of the page. I end up using a few global variables that work fine, but randomly, and I would like to know if there is a better and more elegant way to do this?
source
share