Javascript window.attachEvent不';我好像不在IE8工作

Javascript window.attachEvent不';我好像不在IE8工作,javascript,internet-explorer-8,dom-events,Javascript,Internet Explorer 8,Dom Events,首页: ... <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>
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>
。。。
if(window.addEventListener){
//addEventListener等效于下面的代码
}else if(窗口附件){
window.attachEvent(“消息”,函数(e){
如果(e.origin!=“page2.html”){return;}
警报(如数据);
});
}
第2.html页:

<script>
var message = "hello!";
parent.postMessage(message, '*');
</script>

var message=“你好!”;
parent.postMessage(消息“*”);
这段代码在Chrome、Firefox和Opera中运行良好。当然,IE有自己的处理方式,因此尽管使用了自己的
.attachEvent
,但该代码仍然无法工作

html实际上是另一个域上的页面;我正在发送正确的P3P头(应该没关系,但确实如此)


如何找出
postMessage
似乎没有到达父页面的原因?

attachEvent
“onmessage”
的形式使用其事件名称,而不是
addEventListener
(使用
“message”

…它使用
窗口。event
而不是
event
参数。将第一个参数切换为“onmessage”。。。似乎没有帮助。我在这里遗漏了什么?请参阅@MarcelKorpel的评论,旧版IE使用
window.event
。一个常见的通病是
e=e | | window。event
我已经添加了您刚刚在函数(e)开头编写的小贴士,但它仍然不起作用。非常感谢您,这个答案在搜索了一个多小时后对我有所帮助!