如何利用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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 使用Python和百度語音識別生成視頻字幕的實現(xiàn)2. Gitlab CI-CD自動化部署SpringBoot項目的方法步驟3. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……4. IDEA版最新MyBatis程序配置教程詳解5. python pymysql鏈接數(shù)據(jù)庫查詢結(jié)果轉(zhuǎn)為Dataframe實例6. ASP刪除img標簽的style屬性只保留src的正則函數(shù)7. idea設(shè)置自動導入依賴的方法步驟8. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式9. 教你如何寫出可維護的JS代碼10. 詳解Java內(nèi)部類——匿名內(nèi)部類
