Hello, I thought JavaScript code can be put anywhere in the html code. But it seems to be wrong with the following code:
This is a calculator. I simply moved the script up into the head part. Then it kept giving me errors. Do you guys know what's wrong with it? Thanks a lot.
<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.0//EN">
<HTML>
<HEAD>
<TITLE> From HTML Template </TITLE>
</HEAD>
<BODY BGCOLOR="#FFF00">
<H1 ALIGN="CENTER"> Calculator </H1>
<HR><p>
<TABLE ALIGN="CENTER">
<FORM>
<INPUT TYPE=TEXT NAME=display SIZE=13>
</FORM>
</TABLE>
<TABLE ALIGN="CENTER" BORDER="1" >
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=http://www.webdeveloper.com/forum/archive/index.php/" 1 " onClick="handleClick(1)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 2 " onClick="handleClick(2)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 3 " onClick="handleClick(3)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 4 " onClick="handleClick(4)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 5 " onClick="handleClick(5)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 6 " onClick="handleClick(6)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 7 " onClick="handleClick(7)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 8 " onClick="handleClick(8)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 9 " onClick="handleClick(9)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" = " onClick="handleClick(-1)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 0 " onClick="handleClick(0)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" + " onClick="handleClick(-2)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" * " onClick="handleClick(-3)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" - " onClick="handleClick(-4)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" / " onClick="handleClick(-5)">
</FORM>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide script
var arg = 0, ans = 0, y = 0;
var resultBox = window.document.forms[0];
function handleClick(x)
{
if (0 <= x)
{
arg = 10 * arg + x;
resultBox.display.value = arg;
}
<!-- else
{
if (y == -2) // +
{
ans += arg;
}
else if (y == -3) // *
{
ans *= arg;
}
else if (y == -4) // -
{
ans -= arg;
}
else if (y == -5) // /
{
ans /= arg;
}
else
{
ans = arg;
}
if (x == -1) // =
{
//ans = arg;
y = 0;
}
else
{
y = x;
}
arg = 0;
resultBox.display.value = ans;
}
-->
}
//-->
</SCRIPT>
<P><HR>
</BODY>
</HTML>
This is a calculator. I simply moved the script up into the head part. Then it kept giving me errors. Do you guys know what's wrong with it? Thanks a lot.
<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.0//EN">
<HTML>
<HEAD>
<TITLE> From HTML Template </TITLE>
</HEAD>
<BODY BGCOLOR="#FFF00">
<H1 ALIGN="CENTER"> Calculator </H1>
<HR><p>
<TABLE ALIGN="CENTER">
<FORM>
<INPUT TYPE=TEXT NAME=display SIZE=13>
</FORM>
</TABLE>
<TABLE ALIGN="CENTER" BORDER="1" >
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=http://www.webdeveloper.com/forum/archive/index.php/" 1 " onClick="handleClick(1)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 2 " onClick="handleClick(2)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 3 " onClick="handleClick(3)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 4 " onClick="handleClick(4)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 5 " onClick="handleClick(5)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 6 " onClick="handleClick(6)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 7 " onClick="handleClick(7)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 8 " onClick="handleClick(8)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 9 " onClick="handleClick(9)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" = " onClick="handleClick(-1)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" 0 " onClick="handleClick(0)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" + " onClick="handleClick(-2)">
</FORM>
</TD>
</TR>
<TR>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" * " onClick="handleClick(-3)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" - " onClick="handleClick(-4)">
</FORM>
</TD>
<TD>
<FORM>
<INPUT TYPE="BUTTON" VALUE=" / " onClick="handleClick(-5)">
</FORM>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide script
var arg = 0, ans = 0, y = 0;
var resultBox = window.document.forms[0];
function handleClick(x)
{
if (0 <= x)
{
arg = 10 * arg + x;
resultBox.display.value = arg;
}
<!-- else
{
if (y == -2) // +
{
ans += arg;
}
else if (y == -3) // *
{
ans *= arg;
}
else if (y == -4) // -
{
ans -= arg;
}
else if (y == -5) // /
{
ans /= arg;
}
else
{
ans = arg;
}
if (x == -1) // =
{
//ans = arg;
y = 0;
}
else
{
y = x;
}
arg = 0;
resultBox.display.value = ans;
}
-->
}
//-->
</SCRIPT>
<P><HR>
</BODY>
</HTML>