您的位置:首頁技術文章
文章詳情頁

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

瀏覽:98日期:2024-03-31 18:19:13

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

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中發送數據時

// 發送數據方var domain = ’http://127.0.0.1:10001’;var myPopup = window.open(’http://127.0.0.1:10001/B.html’);myPopup.postMessage(’數據’, domain);// 接收數據方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窗口間通信的詳細內容,更多關于js在兩個html窗口間通信的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
国产综合久久一区二区三区