场景:当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窗口间通信的资料请关注其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)