如何利用js在兩個(gè)html窗口間通信
場(chǎng)景:當(dāng)A頁(yè)面打開(kāi)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)前窗口的打開(kāi)者引用
var domain = ’http://127.0.0.1:10001’;window.opener.postMessage('success', domain);window.close();
如果是需要A打開(kāi)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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python基于openpyxl生成excel文件2. Python基于jieba, wordcloud庫(kù)生成中文詞云3. JS中6個(gè)對(duì)象數(shù)組去重的方法4. python利用蒙版摳圖(使用PIL.Image和cv2)輸出透明背景圖5. win10下opencv-python特定版本手動(dòng)安裝與pip自動(dòng)安裝教程6. 淺談JavaScript的對(duì)象類型之function7. Python web如何在IIS發(fā)布應(yīng)用過(guò)程解析8. 每日六道java新手入門面試題,通往自由的道路--多線程9. Python Sqlalchemy如何實(shí)現(xiàn)select for update10. Java下變量大小寫駝峰、大小寫下劃線、大小寫連線轉(zhuǎn)換
