• iframe 父子间传值通信


    1、同域 iframe 父子间传值

    (1)父页面

    <html>
    <head>
        <script type="text/javascript">
            function say(){
                alert("parent.html");
            }
            function callChild(){
                myFrame.window.say();
                myFrame.window.document.getElementById("button").value="调用结束";
            }
        </script>
    </head>
    <body>
        <input id="button" type="button" value="调用child.html中的函数say()" onclick="callChild()"/>
        <iframe name="myFrame" src="child.html"></iframe>
    </body>
    </html>

    (2)子页面

    <html>
    <head>
        <script type="text/javascript">
            function say(){
                alert("child.html");
            }
            function callParent(){
                parent.say();
                parent.window.document.getElementById("button").value="调用结束";
            }
        </script>
    </head>
    <body>
        <input id="button" type="button" value="调用parent.html中的say()函数" onclick="callParent()"/>
    </body>
    </html>

    总结:方法调用

    父页面调用子页面方法:FrameName.window.childMethod();
    
    子页面调用父页面方法:parent.window.parentMethod();

    2、跨域 iframe 父子间传值

    (1)父页面

    <template>
        <div>
            <iframe 
             :src="iframesrc" 
             id="a-page"></iframe>
        </div>
    </template>
    
    <script>
    export default {
        computed:{
            iframesrc:function(){
                let iframesrc = "http://b.com"
                return iframesrc
            }
        },
        created () {
            // 得到B传来的值 
            window.addEventListener('message',function(e){
                console.log("B DOM的高度", e.data)
            },false);
            // 监听A页面的事件,发送给B
            window.addEventListener('scroll', function () {
                let iframe = document.getElementById('a-page');
                
                let json = {
                    scrollTop: scrollTop,
                    windowHeight: windowHeight,
                };
                iframe.contentWindow.postMessage(json, '*');
            });
        }
    }
    </script>

    (2)子页面

    <template>
        <div>
            <div id="b-page"></div>
        </div>
    </template>
    
    <script>
    export default {
        mounted() {
            // 获取到B页面的值,发送给A
            let _this = this
            let b_pageH = document.getElementById('b-page').scrollHeight;
            window.parent.postMessage(b_pageH, '*');
            // 得到A页面的值
            window.addEventListener('message',function(e){
                console.log("e.data.scrollTop", e.data.scrollTop)
                console.log("e.data.windowHeight", e.data.windowHeight) 
            },false);
        }
    }
    </script>
  • 相关阅读:
    Codeforce-Power Tower(欧拉降幂)
    Caesar Cipher
    BZOJ-1143-祭祀river(二分图-偏序集最大反链)
    商务英语中级第三版 MODULE2 Task 3: Listen to the presentation and write what each refers to.
    计算机网络第一章学习笔记
    第一篇博客随笔
    子页面传递数组给父页面
    第6课第4节_Binder系统_驱动情景分析_服务注册过程_分析
    opencv Mat 与MFC中的CImage相互转换
    多文档中建立一个对话框类,通过这个方法来在其他类中得到对话框对象指针,访问对话框成员
  • 原文地址:https://www.cnblogs.com/dxt510/p/11151744.html
Copyright © 2020-2023  润新知