iframe父子页面通讯解决方案

有两点要注意

onload加载完成再进行操作

子页面延时获取

场景是这样的,父级页面嵌套了多个iframe,report_six.html子级页面按需加载数量,且每个子页面渲染不同的数据。
父页面for循环加载子页面数量,动态生成id,根据对应的id,传值给对应的子级页面元素,一定要写在onload里面

下面是父页面

<body>
    <iframe src="report_one.html" frameborder="0" width="630" height="891"></iframe>
    <iframe src="report_two.html" frameborder="0" width="630" height="891"></iframe>
    <iframe src="report_three.html" frameborder="0" width="630" height="891"></iframe>
    <iframe src="report_four.html" frameborder="0" width="630" height="891"></iframe>
    <iframe src="report_five.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="a_0" src="report_six.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="a_1" src="report_six.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="a_2" src="report_six.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="a_3" src="report_six.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="a_4" src="report_six.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="report_ten" src="report_ten.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="report_eleven" src="report_eleven.html" frameborder="0" width="630" height="891"></iframe>
    <iframe id="report_nine" src="report_nine.html" frameborder="0" width="630" height="891"></iframe>

</body>
<script src="./js/jquery-2.1.4.js"></script>
<script>
    if(obj.type == 3){
        $('<iframe src="report_one.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        $('<iframe src="report_two.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        $('<iframe src="report_three.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        $('<iframe src="report_four.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        $('<iframe src="report_five.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        var rank_a = obj.k.length;
        for(var i = 0;i < rank_a;i++){
            $('<iframe id="a_' + i + '" src="report_six.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        }
        $('<iframe id="report_ten" src="report_ten.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        $('<iframe id="report_eleven" src="report_eleven.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        var cancer = obj.c.length;
        if(cancer > 0){
            $('<iframe id="report_nine" src="report_nine.html" frameborder="0" width="630" height="891"></iframe>').appendTo('body');
        }
        window.onload = function(){
            var rank_a = obj.m07.dis_risk.length;
            for(var i = 0;i < rank_a;i++){
                $('#a_' + i).contents().find('#rank').val(i);
            }
            $('#report_ten').contents().find('#change').html('四');
            $('#report_eleven').contents().find('#change').html('五');
            $('#report_nine').contents().find('#change').html('六');
        }
    }
</script>
子级页面带有默认值的元素input#rank接收数据,为了确定接收到参数,设置一个定时器,不等于默认值的情况下就表示接收成功,接收成功之后清除定时器执行业务代码

下面是子页面

    <body>
        <section>
            <div id="mainChart" class="chart"></div>
        </section>
        <input type="hidden" id="rank" value="a"/>
        <script src="./js/echarts-all-3.js"></script>
        <script>
            var timer = setInterval(function(){
                var rank_value = document.getElementById("rank").value;
                if(rank_value != 'a'){
                    clearInterval(timer);
                    //执行代码
                }
            },2000);
        </script>
    </body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iframe父子页面跨域问题是指在Web开发中,当一个页面(父页面)中嵌套了另一个页面(子页面)的iframe元素,并且两个页面的域名或协议或端口不一致时,就会出现跨域问题。 由于浏览器的同源策略,子页面无法直接访问父页面的内容,也无法与父页面进行通信。这是为了保护用户隐私和安全。但是,有时候我们可能需要在父子页面之间进行数据交互或调用父页面的方法。 解决跨域问题的常见方法有以下几种: 1. 使用postMessage API:在子页面中使用postMessage方法向父页面发送消息,父页面监听message事件来接收消息。这种方式可以实现跨域通信,但需要在两个页面中编写额外的代码。 2. 设置document.domain:如果两个页面的域名只是在子域名上有差异(例如,父页面为example.com,子页面为sub.example.com),可以在两个页面中都设置document.domain为相同的主域名(例如,example.com)。这样浏览器会认为它们同源,从而解决跨域问题。 3. 使用window.name或location.hash:通过修改window.name或location.hash来传递数据。这种方式可以绕过同源策略限制,但只适用于传递较小的数据量。 4. 使用代理页面:在同源的页面中设置一个代理页面,该代理页面作为中间人,用于转发父子页面之间的数据。 需要根据具体的场景和需求选择适合的解决方案来解决iframe父子页面跨域问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值