admin管理员组

文章数量:1293067

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?

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?

Share Improve this question edited May 26, 2020 at 12:27 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 6, 2013 at 20:45 dsp_099dsp_099 6,12120 gold badges78 silver badges131 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

attachEvent takes its event name in the form "onmessage", as opposed to addEventListener (which uses "message")

本文标签: javascriptwindowattachEvent doesn39t seem to be working in IE8Stack Overflow