其他分享
首页 > 其他分享> > 两个非同源页面之间通信,postMessage

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

标签:postMessage,function,127.0,http,0.1,html,3000,同源,页面
来源: https://www.cnblogs.com/Lilc20201212/p/16667918.html