两个非同源页面之间通信,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>

posted on 2022-09-08 02:10  In-6026  阅读(186)  评论(0编辑  收藏  举报

导航