即使我通过 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);
为什么听者从不拾取消息?