js window.open 页面之间的通讯(不同源)

一:a页面
1:打开b页面
let isB= window.open('b.html','b');

2:a页面发送消息给b页面
    isB.postMessage("dsddfsdf", 'b.html');
 
 
  二: b页面
 
       b页面接受a页面的消息
 
 
window.onload = function() {
window.addEventListener('message', function(event) {
 //可以通过event.origin  判断消息来源
 
console.log(event.data,'a发送来的消息');
//作为接受到消息的回应 同样给a页面发送一个消息
   
 
//如果a页面没有关闭
 if(window.opener){
     window.opener.postMessage('我是b,我收到消息了','a.html');
}else{
console.log("a页面关闭了啊");
}
 
});
}
 
 
LAST:
 
在a页面同样添加 接受消息的事件  接受b接受消息的反馈信息
window.onload = function() {
window.addEventListener('message', function(event) {
//其他操作
})
}

  

原文地址:https://www.cnblogs.com/lgjc/p/11136840.html