...

View Full Version : Alert() not working in Firefox or Safari



ApprentiSorcier
07-01-2006, 09:44 AM
I have created a simple grade calculator, and it works very well in Internet Explorer, however, when I access this page from Firefox or Safari, somehow alert() isn't working.

This is the webpage address.
http://homepage.mac.com/yamadatakayoshi/Sites/gradecalculator.html

It is supposed to take inputs from the text field, and then when calculated, it pops up an alert window, indicating the final official grade. But in firefox, this never works.

I will appreciate any help on this matter.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title> Grade Calculator </title>


<SCRIPT type="text/javascript" LANGUAGE="JavaScript">
<!--

function reset() {
document.gradeCalc.HW1.value = 0;
document.gradeCalc.HW2.value = 0;
document.gradeCalc.HW3.value = 0;
document.gradeCalc.HW4.value = 0;
document.gradeCalc.final.value = 0;
}

function calcgrade() {
var grade = new Array(5);
grade[0] = parseFloat(document.gradeCalc.HW1.value);
grade[1] = parseFloat(document.gradeCalc.HW2.value);
grade[2] = parseFloat(document.gradeCalc.HW3.value);
grade[3] = parseFloat(document.gradeCalc.HW4.value);
grade[4] = parseFloat(document.gradeCalc.final.value);
var check = 2; //integer value to validate if float was entered correctly

for(var i=0; i<5; i++)
{
if(isNaN(grade[i]))
{
check = 0; //set false to float validation
}
}

if(check == 0)
{
alert("One or more numeric values have been entered incorrectly. Try again.");
reset();
}
else
{
for(var j = 0; j < 5; j++)
{
if(grade[j] > 100 || grade[j] < 0)
{
check = 1;
}
}
if(check == 1)
{
alert("One or more numeric values have either less than 0 or more than 100. Try again.");
reset();
}
else
{
var max1, max2, max3;
var min1, min2;
max1 = Math.max(grade[0],grade[1]);
max2 = Math.max(grade[2],grade[3]);
min1 = Math.min(grade[0],grade[1]);
min2 = Math.min(grade[2],grade[3]);
max3 = Math.max(min1, min2);

var sumHomework = max1 + max2 + max3;
var finalSumGrade = 0.2 * sumHomework + 0.4 * grade[4];
var finalOfficialGrade = Math.floor(10 * Math.max(finalSumGrade, grade[4])) / 10;
alert("Final Grade is " + finalOfficialGrade + ". Notice that the lowest assignment grade has been dropped.");
}
}
}
//-->
</script>
</head>

<body>
<CENTER>
<FORM Name="gradeCalc">

<TABLE BORDER=2 BGCOLOR=#C0C0C0 CELLPADDING="2" CELLSPACING="2">
<TH></TH>
<TH>Grade</TH>

<TR>
<TD>Assignment 1</TD>
<TD><INPUT TYPE=TEXT SIZE=5 NAME="HW1" ALIGN=TOP
MAXLENGTH=5 VALUE=0></TD>
</TR>

<TR>
<TD>Assignment 2</TD>
<TD><INPUT TYPE=TEXT SIZE=5 NAME="HW2" ALIGN=TOP
MAXLENGTH=5 VALUE=0></TD>
</TR>

<TR>
<TD>Assignment 3</TD>
<TD><INPUT TYPE=TEXT SIZE=5 NAME="HW3" ALIGN=TOP
MAXLENGTH=5 VALUE=0></TD>
</TR>

<TR>
<TD>Assignment 4</TD>
<TD><INPUT TYPE=TEXT SIZE=5 NAME="HW4" ALIGN=TOP
MAXLENGTH=5 VALUE=0></TD>
</TR>

<TR>
<TD>Final Exam</TD>
<TD><INPUT TYPE=TEXT SIZE=5 NAME="final" ALIGN=TOP
MAXLENGTH=5 VALUE=0></TD>
</TR>

<TR ALIGN=CENTER>
<TD COLSPAN=3><INPUT TYPE="BUTTON" VALUE="Calculate"
NAME="calcButton" OnClick="calcgrade()"></TD>
</TR>

<TR ALIGN=CENTER>
<TD COLSPAN=3><INPUT TYPE="BUTTON" VALUE="Reset"
NAME="resetButton" OnClick="reset()"></TD>
</TR>

</TABLE>
</FORM>
</CENTER>

</body>
</html>

Kravvitz
07-01-2006, 11:27 AM
It works in FF1.5 but not in FF1.0.

The error message FF1.0 gives makes me think you should try retyping this line:

document.gradeCalc.final.value = 0;

ApprentiSorcier
07-03-2006, 01:19 AM
Thanks for pointing out that it actually works in Firefox 1.5.

I updated my firefox and yes it does work.

I still find this javascript based webpage broken in Safari, but it is ok.

Thanks for taking time for looking at this massive code!!!

joeframbach
07-03-2006, 03:04 AM
I think the correct DOM usage would be:
document.forms.gradeCalc.name.value;

I may be wrong, though.

Kravvitz
07-03-2006, 03:36 AM
He's using the short form.

You could also use the full DOM0 form which is:

document.forms.gradeCalc.elements.name.value
OR with mixed dot and array syntax

document.forms['gradeCalc'].elements['name'].value



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum