通过一本 JavaScript 书,希望我制作一个简单的阶乘计算器,当我单击下面代码中的计算按钮时,我不断收到错误"此页面未加载到正确的框架集中"。现在代码被编程为如果它不在正确的框架集中,就会抛回此错误,但我的问题是为什么不是?
编辑:澄清一下,我肯定是从calcfactorialtopframe.htm
开始的。
钙化顶架.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>
钙化.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>
,这是Chrome错误,在最新版本的Firefox,IE和Opera:)中工作正常
我的猜测是你被加载calcfactorial.htm
到地址栏中,而不是calcfactorialtopframe.htm
.
Chrome的错误。
我以前确实在IE中尝试了相同的代码,但出现相同的错误,尽管再次尝试后它似乎在IE中工作,但在Chrome中仍然不起作用。