阻止原始帧“空”从访问交叉源框架 - 铬 [英] blocked a frame of origin "null" from accessing a cross-origin frame - chrome
问题描述
我是Javascript新手,正在通过一本教科书来学习基础知识,这本教科书着重介绍其在IE 7+和Firefox 2+中的应用。但是,我使用的是Chrome,并且在运行本书中给出的程序时出现以下错误:阻止原始帧'null'访问跨源帧。任何人都可以告诉我是什么原因导致了错误,我该如何修复它?这两个程序如下。
//这是程序被加载到浏览器中
<!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>示例< / title>
< script type =text / javascript>
函数calcFactorial(factorialNumber){
var factorialResult = 1; (;; factorialNumber> 0; factorialNumber--)factorialResult * = factorialNumber;
;
return factorialResult;
}
< / script>
< / head>
< frameset cols =100%,*>
< frame name =fraCalcFactorialsrc =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>示例< / title>
< script type =text / javascript>
函数butCalculate_onclick(){
尝试{
if(window.top.calcFactorial == null)
throw此页面未在正确的框架集中加载;
if(document.form1.txtNum1.value ==)
throw!请在计算它的阶乘之前输入一个值;
if(isNaN(document.form1.txtNum1.value))
throw!请输入一个有效的数字;
if(document.form1.txtNum1.value< 0)
throw!请输入正数;
$ b $ 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.message);
}
}
< / script>
< / head>
< body>
< form action =name =form1>
< input type =textname =txtNum1size =3/> factorial是
< input type =textname =txtResultsize =25/>< br />
< input type =buttonvalue =Calculate Factorial
name =butCalculateonclick =butCalculate_onclick()/>
< / form>
< / body>
< / html>
这是因为Chrome不允许硬盘访问彼此的内容。其中,从技术上讲,我们将其称为跨源请求。
以上问题的解决方案是:
1.您可以将您的网页托管在本地网络服务器上。请参阅以下链接:
什么是Python的http更快的替代方法.server(或SimpleHTTPServer)?
2.使用Firefox等其他浏览器
I'm new to Javascript and am learning the basics via a textbook that focuses on its applications in IE 7+ and Firefox 2+. However, I am using Chrome and am getting the following error when running the program given in the book: "blocked a frame of origin 'null' from accessing a cross-origin frame." Can anyone tell me what is causing the error and how I can fix it? The two programs are below.
//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>
Below is the src file
<!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>
This happens because Chrome doesn't allow frames from your hard disk to access each others' content. Which, technically we term as Cross-origin request.
Solution of the above problem is:
1. Either you host your webpage on a local web server. See the following link:
What is a faster alternative to Python's http.server (or SimpleHTTPServer)?
2. Use any other browser like Firefox
这篇关于阻止原始帧“空”从访问交叉源框架 - 铬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!