Top page:
前页面:
...
<script>
if (window.addEventListener) {
// addEventListener equivalent of code below
} else if (window.attachEvent) {
window.attachEvent("message", function(e) {
if (e.origin != "page2.html") { return; }
alert(e.data);
});
}
</script>
<iframe src="page2.html"></iframe>
page2.html:
page2.html:
<script>
var message = "hello!";
parent.postMessage(message, '*');
</script>
This code works fine in Chrome, FF and Opera. Of course IE has its own way of doing things so this code doesn't work despite using its own .attachEvent
.
这段代码在Chrome、FF和Opera中运行良好。当然,IE有自己的方法,所以尽管使用了它自己的. attachevent,该代码也不能工作。
The page2.html is actually a page on another domain; I'm sending the correct P3P headers ( shouldn't matter, but there's that ).
所以page2。html实际上是另一个领域的一页;我正在发送正确的P3P标题(不重要,但是有这个)。
How do I find out why postMessage doesn't seem to be reaching the parent page?
我怎么知道为什么postMessage似乎没有到达父页面?
1 个解决方案
#1
11
attachEvent
takes its event name in the form "onmessage"
, as opposed to addEventListener
(which uses "message"
)
attachEvent以“onmessage”的形式接收其事件名称,而不是addEventListener(它使用“消息”)
#1
11
attachEvent
takes its event name in the form "onmessage"
, as opposed to addEventListener
(which uses "message"
)
attachEvent以“onmessage”的形式接收其事件名称,而不是addEventListener(它使用“消息”)