首页 > 解决方案 > window.postMessage 不工作 JavaScript

问题描述

我写了一个非常基本的代码,但它不起作用。请让我知道我的错误。谢谢你。

父方:

<!DOCTYPE html>
<html>
<head>
    <title>Parent</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function() {
        let el_win = window.open("https://www.example.com/child/", "_blank", "toolbar=no, scrollbars=no, resizable=no, top=0, left=0, width="+screen.availWidth+", height="+screen.availHeight);
        el_win.focus();
        el_win.postMessage("hello","*");

    }
</script>
</body>
</html>

儿童侧:

<!DOCTYPE html>
<html>
<head>
    <title>child</title>
</head>
<body>
<script type="text/javascript">
    
window.addEventListener("message", function(e) {
    alert("Message Recieved");
    console.log(e.data);
}, false);
</script>
</body>
</html>

Evenalert()没有在子窗口中调用,也没有数据console.log()

标签: javascriptpostmessage

解决方案


推荐阅读