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) {
//其余操做
})
}
相关文章
相关标签/搜索