欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何利用js在兩個html窗口間通信

 更新時間:2021年04月27日 14:56:16   作者:淺笑·  
這篇文章主要介紹了如何利用js在兩個html窗口間通信,如果讀者們有類似的需求,可以參考下

場景:當A頁面打開B頁面,在B頁面操作后,A頁面需要同步變更數(shù)據(jù)時

A 頁面 ,http://127.0.0.1:10001/A.html

var domain = 'http://127.0.0.1:10001';

window.open('http://127.0.0.1:10001/B.html');
window.addEventListener('message', function (event) {
    if (event.origin !== domain) return;
    console.log('message received:  ' + event.data, event);
}, false);

B 頁面 ,http://127.0.0.1:10001/B.html,opener是當前窗口的打開者引用

var domain = 'http://127.0.0.1:10001';
window.opener.postMessage("success", domain);
window.close();

如果是需要A打開B的同時向B中發(fā)送數(shù)據(jù)時

// 發(fā)送數(shù)據(jù)方
var domain = 'http://127.0.0.1:10001';
var myPopup = window.open('http://127.0.0.1:10001/B.html');
myPopup.postMessage('數(shù)據(jù)', domain);

// 接收數(shù)據(jù)方
window.addEventListener('message', function(event) {
    if(event.origin !== 'http://127.0.0.1:10001') return;
    console.log('message received:  ' + event.data,event);
},false);

以上就是如何利用js在兩個html窗口間通信的詳細內(nèi)容,更多關(guān)于js在兩個html窗口間通信的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論