框架B中的脚本不能引用框架A的Window对象

the script in frame B can't refer to the Window object for frame A

本文关键字:框架 Window 对象 引用 脚本 不能      更新时间:2023-09-26

我有三个文件。首先是index.html,其次是frame1.html,第三是frame2.html。

在index.html我有代码:

<iframe src="frameA.html"></iframe>
<iframe src="frameB.html"></iframe>
在frameA:

<script>
var n = 3;
</script>
在frameB:

console.log(parent[0].n)// undefined why?

在frameB中,我得到msg未定义,但当我运行console.log(parent[0])时,我得到window frameA。为什么我不能从框架a到框架b的值?

在Firefox中,它就像一个魅力。在Chromium/Chrome访问n被同源策略阻止。

Uncaught SecurityError: Blocked a frame with origin "null" from access原点为"null"的帧。

协议、域和端口必须匹配。

如果您将示例上传到任何服务器或设置本地主机,它在Chromium中也可以正常工作(在本地apache2上进行了测试)。

也许Chromium在file:// -protocol上工作时偶然发现了空的window.location.hostname