I have very simple html and js files – which I’ve included below.
What is interesting (and frustrating) is that these files work in Safari – but in Firefox, pressing the Submit button generates this error message:
Error: sFeedback is not defined
Source File: javascript:sFeedback(); Line: 1
Reading other postings like JavaScript function not defined in Firefox?, it seems that Firefox may need to have function defined before it can be called. If this is the case, is there a way – in my setup – to have function defined prior to call?
quiz.html:
<!DOCTYPE html>
<html>
<head>
<title>Math Quiz</title>
<meta name="generator" content="BBEdit 9.6" />
</head>
<script language="JavaScript" src="generateQuizItem.js">
</script>
<body onLoad="javascript:sGenerateQuizItem();">
</body>
</html>
And javascript:
var a0;
var a1;
var sum;
function sFeedback()
{
var answer = document.quiz_form.answer.value;
document.write("answer = " + answer + "<br>");
if (answer == sum)
document.write("Right!<br>");
else
document.write("Wrong!<br>");
}
function sGenerateQuizItem()
{
a0 = 20;
a1 = 40;
sum = a0 + a1;
document.write("<form action=\"javascript:sFeedback();\" method=\"get\" name=\"quiz_form\">\n");
document.write(a0 + " + " + a1 + " = ");
document.write("<input name=\"answer\" id=\"answer\" type=\"text\" size=\"4\" maxlength=\"4\" /><br><br>\n");
document.write("<button type=\"submit\" id=\"submit\">Submit</button>");
document.write("</form>");
}
Try putting the script reference within the head of your document.