无法从Iframe访问父文档 [英] Can't Access Parent-document from Iframe

查看:105
本文介绍了无法从Iframe访问父文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主页面,用于导航到 iframe

<主页: 1.html

其他3页: one.html 二。 html 3.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>

但是,显示父级 -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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆