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

如何利用js在兩個(gè)html窗口間通信

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

場(chǎng)景:當(dāng)A頁(yè)面打開B頁(yè)面,在B頁(yè)面操作后,A頁(yè)面需要同步變更數(shù)據(jù)時(shí)

A 頁(yè)面 ,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 頁(yè)面 ,http://127.0.0.1:10001/B.html,opener是當(dāng)前窗口的打開者引用

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

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

// 發(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在兩個(gè)html窗口間通信的詳細(xì)內(nèi)容,更多關(guān)于js在兩個(gè)html窗口間通信的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論