博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
谷歌浏览器的一个新特点—关于获取iframe的parent对象
阅读量:4635 次
发布时间:2019-06-09

本文共 1396 字,大约阅读时间需要 4 分钟。

版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.csdn.net/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" οnclick="javascript:window.parent.test();" value="调用iframe标签所在页面的函数"/>

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

                  </body>

       </html>

       【】

       假设上面代码在电脑上直接使用谷歌浏览器执行就会出现上面的错误“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

你可能感兴趣的文章
京华同学聚会
查看>>
JSP--JavaBean
查看>>
成功将BlogEngine 1.5 升级到了BlogEngine 2.0
查看>>
【shell】创建长目录,目录存在则忽略,缺失则创建
查看>>
个人随笔、收藏——(包括技术、设计思想等)
查看>>
第一段冲刺_个人总结_5.2
查看>>
Usage and Idioms——Categories
查看>>
一: 建立Vue sampleproject
查看>>
数据结构之shell排序
查看>>
CodeForces 375D Tree and Queries
查看>>
牛客~~打篮球~~~模拟水题
查看>>
LeetCode-198. 打家劫舍
查看>>
5 -- Hibernate的基本用法 --2 1 Hibernate 下载和安装
查看>>
Socket
查看>>
【C#公共帮助类】10年代码,最全的系统帮助类
查看>>
JQuery UI
查看>>
张弛有度
查看>>
【ZJOI2008】树的统计(树链剖分)
查看>>
【NOIP校内模拟】T2 华莱士(环套树)
查看>>
lists,tuples and sets of Python
查看>>