html5 postMessage无效

时间:2012-01-25 19:36:57

标签: javascript html html5 cross-domain postmessage

即使我通过postMessage重复发送域消息,我的window.addEventListener的函数也不会被触发。它似乎根本没有收到任何回应。这是代码:

var myUrl = 'http://localhost:8085';
var newPopup = window.open( myUrl, '_blank', '' );

// Create listener
window.addEventListener('message',function(event) {

  if (event.origin !== 'http://localhost:8085') return;
    console.log('received response:  ',event.data);

},false);

// Setup messenging
setInterval(function(){
    var message = 'Hello!  The time is: ' + (new Date().getTime());
    console.log('blog.local:  sending message:  ' + message);
    newPopup.postMessage(message,'http://localhost:8085');  
},2000);

为什么听众不接收消息?

1 个答案:

答案 0 :(得分:0)

您正在错误的窗口上注册处理程序。尝试

newPopup.addEventListener('message',function(event) {
    ...