从父级发送postMessage到具有不同来源的iframe

时间:2020-07-14 12:56:22

标签: javascript iframe postmessage

您可能像我曾经遇到过的与iframe进行通信的“同源起源”政策问题一样

SecurityError: Blocked a frame with origin "http://www.<domain>.com" from accessing a cross-origin frame.

我这样做的方式是:

this.source = iframe.documentWindow
....
post(message) {
  this.source.postMessage(message, origin)
}

那是之前触发错误的地方。

但是,以下代码可以工作,我不明白为什么:

this.source = iframe
....
post(message) {
  this.source.documentWindow.postMessage(message, origin)
}

有人可以向我解释吗?谢谢

0 个答案:

没有答案