D
David
Sorry about the newbie question, but I am getting started with
JavaScript, and I can't get past a hump with global variables. I
declare a variable at the beginning of my script and change it in a
function, but when I try to see it in another function, the changes
don't hold. It seems that the browser is reloading the page each time
I press a button. How can I prevent this? Here is an example of my
code. Thanks for your help.
--David
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
var myCount=0;
//alert(myCount);
function AddOne()
{
alert("The count is " + myCount);
myCount++;
alert("The count is " + myCount);
}
function AddTwo()
{
alert("The count is " + myCount);
myCount = myCount + 2;
alert("The count is " + myCount);
}
-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="TheCount">
<INPUT TYPE="submit" NAME="Add1" VALUE="Add 1"
ONCLICK="AddOne()">
<INPUT TYPE="submit" NAME="Add2" VALUE="Add 2"
ONCLICK="AddTwo()">
</FORM>
</BODY>
</HTML>
JavaScript, and I can't get past a hump with global variables. I
declare a variable at the beginning of my script and change it in a
function, but when I try to see it in another function, the changes
don't hold. It seems that the browser is reloading the page each time
I press a button. How can I prevent this? Here is an example of my
code. Thanks for your help.
--David
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
var myCount=0;
//alert(myCount);
function AddOne()
{
alert("The count is " + myCount);
myCount++;
alert("The count is " + myCount);
}
function AddTwo()
{
alert("The count is " + myCount);
myCount = myCount + 2;
alert("The count is " + myCount);
}
-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="TheCount">
<INPUT TYPE="submit" NAME="Add1" VALUE="Add 1"
ONCLICK="AddOne()">
<INPUT TYPE="submit" NAME="Add2" VALUE="Add 2"
ONCLICK="AddTwo()">
</FORM>
</BODY>
</HTML>