无法从Iframe访问父文档 [英] Can't Access Parent-document from Iframe
问题描述
我有一个主页面,用于导航到 iframe
<主页: 1.html
代码
html
< html>
< body>
< div>
< ul>
< li style =float:left; margin:20px;>< a href =One.htmltarget =target> One< / a>< / li>
< li style =float:left; margin:30px;>< a href =Two.htmltarget =target> Two< / a>< / li>
< li style =float:left; margin:50px;>< a href =Three.htmltarget =target> Three< / a>< / li>
< / ul>
< iframe id =target>< / iframe>
< / div>
< / body>
< / html>
one.html
< HTML>
< body onload =try {alert(parent.location.href);} catch(e){alert(e);}>
1.html。
< / body>
< / html>
但是,显示父级$ c的位置存在问题$ C> -document。它提供了一个警告消息,如下所示:
SecurityError:阻止源null访问原始帧null 。协议,域和端口必须匹配。
任何人都可以在此错误的上下文中解释消息中使用的术语的含义,为什么发生这种情况???
注意:没有使用服务器
Chrome严格地假设本地文件之间没有任何关联。
在Mac上,您可以从终端启动Chrome,如下所示:
/ Applications / Google\ Chrome.app/Contents/MacOS/Google\ Chrome - 允许文件从文件访问
它可以工作,但最好的办法是使用网络服务器。
I have a main page for navigating to other 3 pages in an iframe
Main page: 1.html
Other 3 pages: one.html, two.html, 3.html.
Code
1.html
<html>
<body>
<div>
<ul>
<li style="float:left; margin:20px;"><a href="One.html" target="target">One</a></li>
<li style="float:left; margin:30px;"><a href="Two.html" target="target">Two</a></li>
<li style="float:left; margin:50px;"><a href="Three.html" target="target">Three</a></li>
</ul>
<iframe id="target"></iframe>
</div>
</body>
</html>
one.html
<html>
<body onload="try{alert(parent.location.href);} catch(e){alert(e);}">
1.html.
</body>
</html>
But there is a problem showing the location of the parent
-document. It's giving an alert message as follws:
SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
Can anyone please explain the meaning of the terms used in the message in the context of this error or simply why this happend???
N.B.: No Server Used
Chrome rather strictly assumes that there is no connection between local files.
On a Mac you can start Chrome from the terminal like this:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files
and it will work, but the best thing would be to use a webserver.
这篇关于无法从Iframe访问父文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!