Reputation: 6121
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:
<script>
var message = "hello!";
parent.postMessage(message, '*');
</script>
This code works fine in Chrome, Firefox and Opera. Of course IE has its own way of doing things so this code doesn't work despite using its own .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).
How do I find out why postMessage
doesn't seem to be reaching the parent page?
Upvotes: 2
Views: 5119
Reputation: 324630
attachEvent
takes its event name in the form "onmessage"
, as opposed to addEventListener
(which uses "message"
)
Upvotes: 11