iframe 父子页面之间的通信

作为一个没有什么情怀和追求的前端工程师, 是么有怎么考虑过 iframe 用法的, (毕竟从学习到工作自己扮演的一直是"前端小妹妹"的角色, 这种脏活累活一般是交给后端臭屌丝的,直到公司的全栈妹子来找我探讨这个跨域通信的问题);

真是不看不知道, 一看吓一跳. 原来 iframe 也存在跨域的问题. 跨域和不跨域情况下和父级页面的通信方式也是略有不同.

文中示例代码均在github

同域情况下的父子页面通信方式

show you the demo

父级页面:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>   
  <script>
    function say() {
      console.log("i am father page");
    }

    function callChild() {
      mySon.window.say();
      mySon.window.document.getElementById('button').innerHTML = '调用结束';
    }
  </script>
  <button id="button" onclick="callChild()"> 调用儿子的方法 </button>
  <iframe name="mySon" src="http://localhost:3000/demo.html?aim=xiaopang" frameborder="0"></iframe>
</body>
</html>

儿子页面

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

  <script>
    function say() {
      console.log('i am child page');
    }
    function callFather() {
      parent.say();
      parent.window.document.getElementById('button').innerHTML = '调用结束'
    }
  </script>

  <button id="button" onclick="callFather()"> 调用爸爸的方法 </button>
</body>
</html>

以上示例功能

  • 调用函数:

    1. 父级页面调用子页面的函数 name.window.func();
    2. 子页面调用父级页面的函数 parent.window.func();
  • 访问页面元素
    window都拿到了,元素不会拿。请自行百度。

ps: 注意事项:

儿子页面加载完成后再进行访问, 判断方法:

  1. iframe 上添加 onload 事件;
  2. document.readyState == 'complete';

跨域情况下父子页面通信的方法

如果 iframe 所链接的是外部的页面, 尼玛, 居然有跨域限制, 有跨域限制, 有跨域限制......

父页面向子页面传递数据

跨域情况下的父子页面通信的原理大概就是使用 hash / 直接传参数搞定;倾向于hash;

  1. 在子页面中通过定时器监听 location.hash 的变化就能够拿到父级页面传来的数据啦;
  2. 一次性的从父页面 get 过来的参数中提取有用的部分,就 OK 啦;

子页面向父页面传递数据

跨域情况下,子页面向父页面传递数据的实现原理是使用一个代理的 iframe (就像是跨域请求中的代理服务器一样), 并且保持代理的子页面和父级页面是同域的此时就可以通过, window.top 或者 window.parent.parent 就可以获取父级页面的引用啦. 哔哩哔哩...

不 bb 上 demo

父级页面:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
   我是彩笔圈圈

   <script>
     var i = 1;
     function say(data) {
       console.log('我是爸爸' + data);
     }

     function add() {
       document.getElementById('quanquan').setAttribute('src', 'http://localhost:3000/demo2.html?aim=quanquan#'+ ++i)
     }

   </script>
   <button onclick="add()">+1</button>
   <iframe id='quanquan' src="http://localhost:3000/demo2.html?aim=quanquan#1" frameborder="0"></iframe>
</body>
</html>

子级页面:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    var hash = location.hash, i = 1;
    function checkData() {
      if(hash !== location.hash) {
        hash = location.hash
        console.log('我是儿子' + hash);
      } 
    }
    function say() {
      console.log('i am child page');
    }
    function callFather() {
      document.getElementById('quanquan').setAttribute('src', 'http://localhost:4000/proxy.html#'+ ++i)
    }
    setInterval(checkData, 1e3);
  </script>
  <button id="button" onclick="callFather()"> 调用爸爸的方法 </button>
  <iframe id="quanquan" style="display: none" src="http://localhost:4000/proxy.html#1" frameborder="0"></iframe>
</body>
</html>

proxy 中转页面:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>proxy</title>
</head>
<body>
  <script>
    var hash = location.hash;
    function callGrandFather(data) {
      window.top.say(data);
    };
    function checkData() {
      if ( hash !== location.hash ) {
        callGrandFather(location.hash);
        hash = location.hash;
      }
    }
    setInterval(checkData, 1e3);
  </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 什么是跨域? 跨域一词从字面意思看,就是跨域名嘛,但实际上跨域的范围绝对不止那么狭隘。具体概念如下:只要协议...
    w_zhuan阅读 543评论 0 0
  • 1. 什么是跨域? 跨域一词从字面意思看,就是跨域名嘛,但实际上跨域的范围绝对不止那么狭隘。具体概念如下:只要协议...
    他在发呆阅读 830评论 0 0
  • 前言 原文地址:前端跨域总结 博主博客地址:Damonare的个人博客 相信每一个前端er对于跨域这两个字都不会陌...
    秦至阅读 1,423评论 4 51
  • 转载:http://damonare.github.io/2016/10/30/%E5%89%8D%E7%AB%A...
    壮哉我大前端阅读 548评论 2 9
  • 你说鸢尾寄寂,与子相惜;后来花开如海,两生相忆。 你说引弓落月,子规哀啼;后来天地玄黄,潇潇暮雨。 你说烟雨画堂,...
    木笔乔乔阅读 529评论 0 1