我有两个HTML文件。第一种称为post.html
和它注册的窗口,并发布消息的窗口上的事件侦听器:
<html>
<head>
<script>
window.addEventListener("message", function(event) {
console.log("found event in post!", event)
});
window.postMessage({
'data': ["some data"]
},"*");
</script>
</head>
<body>
</body>
</html>
我有一个名为listner.html
第二HTML文件,只是监听来自窗口和负载post.html
作为一个iframe的消息:
<html>
<head>
<script>
window.addEventListener("message", function(event) {
console.log("event found in listen!", event)
});
</script>
</head>
<body>
<iframe src="post.html"></iframe>
</body>
</html>
如果我加载listener.html
,我得到的日志表明在post.html
事件监听器检测到发布到post.html
窗口中的消息,但我没有得到任何listener.html
日志。我的理解是,我应该能够通过这两个文件,并在window
事件侦听器应该接收事件之间的listener.html
对象进行通信。任何想法,为什么这是不工作?
post.html
被张贴消息到自己的窗口,而不是包含窗口listener.html
window.parent.postMessage(...)