浏览一本希望我制作一个简单的阶乘计算器的 javascript 书籍,当我单击下面代码中的计算按钮时,我不断收到错误“此页面未在正确的框架集中加载”。现在,如果代码不在正确的框架集中,代码被编程为抛出此错误,但我的问题是为什么不是?
编辑:澄清一下,我肯定是从calcfactorialtopframe.htm
.
calcfactorialtopframe.htm
<html>
<head>
<title>Example</title>
<script type="text/javascript">
function calcFactorial(factorialNumber)
{
var factorialResult = 1;
for (; factorialNumber > 0; factorialNumber--)
{
factorialResult = factorialResult * factorialNumber;
}
return factorialResult;
}
</script>
</head>
<frameset cols="100%,*">
<frame name="fraCalcFactorial" src="calcfactorial.htm" />
</frameset>
</html>
calcfactorial.htm
<html>
<head>
<title>Example</title>
<script type="text/javascript">
function butCalculate_onclick()
{
try
{
if (window.top.calcFactorial == null)
throw "This page is not loaded within the correct frameset";
if (document.form1.txtNum1.value == "")
throw "!Please enter a value before you calculate its factorial";
if (isNaN(document.form1.txtNum1.value))
throw "!Please enter a valid number";
if (document.form1.txtNum1.value < 0)
throw "!Please enter a positive number";
document.form1.txtResult.value =
window.parent.calcFactorial(document.form1.txtNum1.value);
}
catch(exception)
{
if (typeof(exception) == "string")
{
if (exception.charAt(0) == "!")
{
alert(exception.substr(1));
document.form1.txtNum1.focus();
document.form1.txtNum1.select();
}
else
{
alert(exception);
}
}
else
{
alert("The following error occursed " + exception.message);
}
}
}
</script>
</head>
<body>
<form action="" name="form1">
<input type="text" name="txtNum1" size="3" /> factorial is
<input type="text" name="txtResult" size="25" /><br />
<input type="button" value="Calculate Factorial" name="butCalculate" onclick="butCalculate_onclick()" />
</form>
</body>
</html>