如何利用js在兩個(gè)html窗口間通信
場(chǎng)景:當(dāng)A頁面打開B頁面,在B頁面操作后,A頁面需要同步變更數(shù)據(jù)時(shí)
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是當(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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖2. JSP頁面的靜態(tài)包含和動(dòng)態(tài)包含使用方法3. 通過Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例4. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析5. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁6. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效7. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟8. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車9. .net如何優(yōu)雅的使用EFCore實(shí)例詳解10. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)
