谷歌浏览器的一个新特点—关于获取iframe的parent对象

谷歌浏览器iframe跨域访问问题
探讨了在谷歌浏览器中,使用file协议访问带有iframe标签的页面时遇到的跨域访问限制问题。当从iframe尝试访问其父页面的parent对象时,会触发安全策略阻止,需通过服务器如Tomcat发布解决。
版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.youkuaiyun.com/wangshuxuncom/article/details/25781445

        谷歌浏览器在调试JavaScript脚本方面非常方便,今天在使用谷歌浏览器调试代码时无意间发现了一个奇特的问题:Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.,这主要是没有获取iframe所在所在页面的parent的造成的,代码例如以下:

        index.html:

       <html>

                 <head>

                            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

                            <script type="text/javascript">

                                         function test() {

                                                alert('您成功调用了iframe标签所在页面的函数!');

                                         }

                           </script>

                 </head>

                 <body>

                          <iframe frameborder="1" src='child.html' style="width:50%; height:50%;" ></iframe>

                          <div style="font-size: 36px; margin-left: 40%; color: red;">iframe标签所在页面</div>

                 </body>

       </html>

       child.html:

       <html>

                   <head>

                           <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

                   </head>

                  <body>

                          <input type="button" onclick="javascript:window.parent.test();" value="调用iframe标签所在页面的函数"/>

                          <div style="font-size: 21px; margin-left: 40%; color: red;">iframe标签所引用的页面</div>

                  </body>

       </html>

       【0分下载代码文件

       假设上面代码在电脑上直接使用谷歌浏览器执行就会出现上面的错误“Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols,domains, and ports must match.”。而使用诸如IE浏览器直接訪问则不会出现这样的情况。假设将其使用server(比方Tomcat)公布一下。再次使用谷歌浏览器通过http訪问index.html文件则不会出现这样的情况。

       总结:假设在本地用file协议使用谷歌浏览器打开含有iframe标签的页面。则被iframe标签引用的页面无法訪问iframe标签所在页面的parent对象,除非使用server(比方Tomcat)公布一下。

转载于:https://www.cnblogs.com/xfgnongmin/p/10851136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值