window.postmessage

Posted l8l8

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了window.postmessage相关的知识,希望对你有一定的参考价值。

<html>
    <head>
        <title>test</title>
        <style>
            iframe
                border: 1px solid red;
            
        </style>
    </head>
    <body>
        <iframe src="./content.html" frameborder="0" id=‘receiver‘></iframe>
        <script>
            window.onload = () => 
                var  receiverWindow = document.getElementById(‘receiver‘).contentWindow;
                receiverWindow.postMessage(‘hello‘,‘*‘)
            
        </script>
    </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>
    <p>postmessage</p>
    <script>
        window.addEventListener(‘message‘,function(e)
            console.log(‘message is ok‘,e.data);
        ,false)
    </script>
</body>
</html>

 

以上是关于window.postmessage的主要内容,如果未能解决你的问题,请参考以下文章