一个页面经过iframe引入另一个页面,页面之间如何通信?两个页面就存在两个window,存在跨域。父子页面能够经过window.postMessage进行通信。html
一、子页面使用window.postMessage通信;跨域
二、父页面使用window.addEventListener("message", receiveMessage, false);实时接收子页面的数据。post
父窗口spa
<iframe src="test2.html"></iframe> <script> function receiveMessage(e) { alert(e.data); } window.addEventListener("message", receiveMessage, false); </script>
子窗口test2.html
.net
<input type="text" value="send" id="input" /> <input type="button" value="send" id="button" /> <script> document.getElementById('button').onclick = function () { top.postMessage(document.getElementById('input').value, '*'); }; </script>
参考地址code