iframe跨域数据传递

项目中需要和其他单位合作开发,方案采用iframe嵌入页面,开发过程中设计到了跨域数据的传递,初步方案决定使用html5 API postMessage进行iframe跨域数据传递;
域名A下的页面

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'/>
    <title>Page_1</title>
</head>
<body style="background: palegoldenrod;">
<div style="width:100%;height:100px;">这是父页面</div>
<div id="cont_father"></div>
<iframe id="page_1" src="http://192.168.100.40:4200/page2/index.html" width="100%"  style="padding:0px; min-height:580px;"></iframe>
<div id="btn_father" style="border:1px solid #999;padding: 10px 50px;display: inline-block;cursor:pointer">打开console,点击后向子页面发送数据</div>
<script>
    window.onload=function () {
        var btn_father=document.getElementById('btn_father');
        var cont_father=document.getElementById('cont_father');
        var sendData={
            text:'这是父页面的数据'
        }
        
        bind(btn_father,'click',function () {
            window.frames[0].postMessage(JSON.stringify(sendData),'http://192.168.100.40:4200/page2/index.html');//数据发送
        })
        
        bind(window,'message',function(e) {
            var e=window.event || arguments[0];
            var data = JSON.parse(e.data);
            console.log('我是父页面,这是子页面发送的数据:'+data);
            console.log(data.text)
            cont_father.innerHTML=data.text;
            console.log('-------------------')
        })
        function bind(obj, evname, fn) {
            if (obj.addEventListener) {
                obj.addEventListener(evname, fn, false);
            } else {
                obj.attachEvent("on" + evname, function () {
                    fn.call(obj);
                })
            }
        }
    }
</script>
</body>
</html>

域名B下的页面

<!doctype html>
<html>
<head>
    <title>Page_2</title>
    <meta charset="utf-8">
</head>
<body style="background: #fff">
<h3>这是子页面</h3>
<div id="cont_child"></div>
<div id="btn_child" style="border:1px solid #eee;padding: 10px 50px;display: inline-block;cursor:pointer">打开控制台,并点击</div>

<script>

    window.onload=function(){

        var btn_child=document.getElementById("btn_child");
        var cont_child=document.getElementById("cont_child");
        var sendData={text:'我是iframe里面的数据'};
        
        bind(btn_child,'click',function(){
            window.parent.postMessage(JSON.stringify(sendData),'http://192.168.100.40:4100/page1/index.html');//数据发送
        })

        bind(window,'message',function(e) {
            var e=window.event || arguments[0];
            if(e.source!=window.parent) return;
            var data = JSON.parse(e.data);
            console.log('我是子页面,这是父页面发送的数据:'+data);
            console.log(data.text)
            cont_child.innerHTML=data.text;
            console.log('------------------')
        })

        function bind(obj, evname, fn) {
            if (obj.addEventListener) {
                obj.addEventListener(evname, fn, false);
            } else {
                obj.attachEvent("on" + evname, function () {
                    fn.call(obj);
                })
            }
        }
    }
</script>
</body>
</html>

经测试,可以兼容ie8

转载于:https://www.cnblogs.com/lianjq/p/7264166.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值