gpt4 book ai didi

javascript - 阻止原始框架 "null"访问跨域框架 - chrome

转载 作者:可可西里 更新时间:2023-11-01 01:38:32 25 4
gpt4 key购买 nike

我是 Javascript 的新手,我正在通过一本侧重于其在 IE 7+ 和 Firefox 2+ 中的应用程序的教科书来学习基础知识。但是,我使用的是 Chrome,在运行书中给出的程序时出现以下错误:“阻止了来自‘null’的来源框架访问跨域框架。”谁能告诉我导致错误的原因以及如何解决?这两个程序如下。

//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>

下面是源文件

<!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>

最佳答案

发生这种情况是因为 Chrome 不允许您硬盘上的帧访问彼此的内容。从技术上讲,我们将其称为跨域请求。

上述问题的解决方法是:

1. 要么将您的网页托管在本地网络服务器上。请参阅以下链接:
What is a faster alternative to Python's http.server (or SimpleHTTPServer)?

2. 使用任何其他浏览器,如 Firefox

关于javascript - 阻止原始框架 "null"访问跨域框架 - chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29983786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com