在沙箱中识别消息中的发件人 iframe
为了简化而虚构的情况:
- 我有一个
grandparent
页面,其中包含一个显示“父页面”的parent
iframe。 - 此“父页面”包含两个子 iframe
childA
和childB
。 - 出于安全原因,所有 iframe 均为
sandbox="allow-scripts"
。
parent
向祖父母
、childA
和childB
发送和接收消息。
当 parent
收到消息时,我需要能够识别发送者,但我找不到正确的方法,因为 event<的
origin
/code> is null
:
function onReceive(message) {
// origin is null when the message comes from a sandboxed frame.
console.log(message.origin);
}
您知道一种干净的方法来知道消息来自谁吗?
Fictional situation for simplification:
- I have a
grandparent
page containing oneparent
iframe displaying the "parent page". - This "parent page" contains two children iframes
childA
andchildB
. - All the iframes are
sandbox="allow-scripts"
for security reasons.
parent
sends and receive messages from/to grandparent
, childA
and childB
.
When parent
receives a message, I need to be able to identify the sender, but I can't find the right approach, as the origin
of the event
is null
:
function onReceive(message) {
// origin is null when the message comes from a sandboxed frame.
console.log(message.origin);
}
Do you know a clean way to know who does the message come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,比我想象的要简单。
我实际上可以将
message.source
属性与子iframes
的contentWindow
或window.parent
进行比较祖父母。OK, it was simpler than I expected.
I actually can compare the
message.source
property with thecontentWindow
of the childiframes
, orwindow.parent
for the grand parent.