2015-05-01 55 views
7

我是Javascript的新手,并且通过一本教科书学习基础知识,重点介绍其在IE 7+和Firefox 2+中的应用。但是,我使用的是Chrome,并且在运行本书中给出的程序时出现以下错误:“阻止原始帧'null'访问跨源帧”。任何人都可以告诉我是什么原因导致了错误,我该如何修复它?这两个程序如下。阻止源框架“null”访问一个跨源框架 - chrome

//This is the program being loaded into the browser 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Example</title> 

<script type="text/javascript"> 

function calcFactorial(factorialNumber){ 
    var factorialResult = 1; 
    for(;factorialNumber>0;factorialNumber--) factorialResult *= factorialNumber; 
    return factorialResult; 
} 

</script> 

</head> 

<frameset cols="100%,*"> 
<frame name="fraCalcFactorial" src="calcfactorial.htm"/> 
</frameset> 

</html> 

下面是在src文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<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"; 
    } 
    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 occurred: " + 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> 
+0

你是从本地机器上运行这个,或者托管的Web服务器上? – CodingIntrigue

+0

默认情况下,Chrome将不允许从本地硬盘加载的帧访问彼此的内容。如果这些框架在同一个Web服务器上,他们可以。 Chrome有一个命令行参数,将暂时暂停此安全检查。 – jfriend00

回答

相关问题