'Identify the sender-iframe from message when sandboxed

Fictional situation for simplification:

  • I have a grandparent page containing one parent iframe displaying the "parent page".
  • This "parent page" contains two children iframes childA and childB.
  • 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?



Solution 1:[1]

OK, it was simpler than I expected. I actually can compare the message.source property with the contentWindow of the child iframes, or window.parent for the grand parent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phasm