多个iframe和父级互相通信,获取iframe框架里面的document对象

多个iframe和父级互相通信

请添加图片描述

  • father.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        iframe {
            width: 100%;
        }
    </style>
</head>
<body>
    <iframe src="./son1.html" frameborder="0"></iframe>
    <iframe src="./son2.html" frameborder="0"></iframe>

    <script>
        // 1. 定义子组件传参类型
        window.iframeNameData = {
            SON1: 'SON1',
            SON2: 'SON2',
        }
        // 2. 定义子组件发送函数
        window.iframeSendMessage = function(type, data) {
            window.parent.postMessage({
                type,
                data
            })
        }
        // 3. 获取iframeDom对象
        const [son1, son2] = document.querySelectorAll('iframe')
        // 4. 监听iframe通知事件
        function receiveMessageIframePage(messageEvent) {
            const type = messageEvent.data.type
            switch (type) {
                case  window.iframeNameData.SON1:
                    console.log(`父组件接收son1信息:`,messageEvent.data)
                    // iframeDom对象通过contentWindow属性获取iframe的window对象
                    // 调用子组件方法传递信息
                    son1.contentWindow.receiveFatherInfo('父组件发送给son1的信息')
                    break;
                case  window.iframeNameData.SON2:
                    console.log(`父组件接收son2信息:`,messageEvent.data)
                    son1.contentWindow.receiveFatherInfo('父组件发送给son2的信息')
                    break;
                
                default:
                    throw new Error('传入了错误类型')
                    break;
            }
        }
        window.addEventListener('message', receiveMessageIframePage, false)
    </script>
</body>
</html>
  • son1.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    hello, 我是son1
    <script>
        function receiveFatherInfo(info) {
            console.log(`我是子组件son1:${info}`)
        }
        setTimeout(() => {
            window.parent.iframeSendMessage(window.parent.iframeNameData.SON1, {
                msg: 'hello, 我是son1'
            })
        }, 1500)
    </script>
</body>
</html>
  • son2.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    hello, 我是son2
    <script>
        function receiveFatherInfo(info) {
            console.log(`我是子组件son2:${info}`)
        }

         setTimeout(() => {
            window.parent.iframeSendMessage(window.parent.iframeNameData.SON1, {
                msg: 'hello, 我是son2'
            })
        }, 3000)
    </script>
</body>
</html>

最后,启动方式不能双击打开,请按照https://blog.csdn.net/weixin_45326220/article/details/134081446方式启动项目

获取iframe框架里面的document对象

iframe框架里面的网页,使用iframe节点的contentDocument属性。

document.defaultView属性返回document对象所属的window对象。如果当前文档不属于window对象,该属性返回null。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值