两个非同源页面之间通信,postMessage
http://127.0.0.1:3000/a.html
<iframe id="frame" src="http://127.0.0.1:3001/b.html"></iframe>
<script>
iframe.onload = function() {
iframe.contentWidnow.postMessage('msg from 3000/a.html', 'http://127.0.0.1:3001/')
}
// 监听b.html传来的信息
window.onmessage = function(e) {
console.log(e.data)
}
</script>
http://127.0.0.1:3001/b.html
<script>
// 监听b.html传来的信息
window.onmessage = function(e) {
console.log(e.data)
e.source.postMessage('send msg to 3000/a.html', e.origin)
}
</script>