在正确的框架集中

在正确的框架集中

当我单击下面代码中的“计算”按钮时,浏览一本希望我做一个简单的阶乘计算器的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>

最佳答案

是的,这是Chrome错误,在Firefox,IE和Opera的最新版本中工作正常:)

10-06 04:43