Global Variables Newbie Question

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>
 
G

Grant Wagner

David said:
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">


Not needed.
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);
}
-->

Should be //-->, but it's not required, remove it entirely.
</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>

The page reloads because clicking an <input type="submit" ...> _submits
the form_. Since the <form ...> has no -action- attribute, it submits to
itself.

Use <input type="button" ...> or <button ...>...</button> (not supported
by some older browsers) if you just want to perform a client-side task
with a button.
 
E

Evertjan.

Grant Wagner wrote on 26 okt 2004 in comp.lang.javascript:
The page reloads because clicking an <input type="submit" ...> _submits
the form_. Since the <form ...> has no -action- attribute, it submits to
itself.

I should have seen that one!!!
Use <input type="button" ...> or <button ...>...</button> (not supported
by some older browsers) if you just want to perform a client-side task
with a button.

or

ONCLICK="AddOne();return false"
....
ONCLICK="AddTwo();return false"
 
M

Martin Walke

Hi David,

It's your use of TYPE="submit" that's causing the problems. The page is
being submitted which causes an effective refresh (please correct me here if
necessary).

Use the BUTTON tag if you want to see a button or something similar.

<button NAME="Add1" ONCLICK="AddOne()">Add one</button>
<button NAME="Add2" ONCLICK="AddTwo()">Add two</button>

or
<input type=button NAME="Add1" VALUE="Add 1" ONCLICK="AddOne()">
<input type=button NAME="Add2" VALUE="Add 2" ONCLICK="AddTwo()">



HTH
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top