You could use e.originalEvent.origin
to identify the iframe.
On the iframe child:
window.parent.postMessage({
'msg': 'works!'
}, "*");
On the iframe parent:
Javascript
window.addEventListener('message', function(e) {
console.log(e.origin); // outputs "http://www.example.com/"
console.log(e.data.msg); // outputs "works!"
if (e.origin === 'https://example1.com') {
// do something
} else if (e.origin === 'https://example2.com'){
// do something else
}
}, false);
jQuery
$(window).on('message', function(e) {
...
}, false);
So origin
contains the protocol and domain from which the postMessage()
was fired from. It does not include the URI. This technique assume all iframes have a unique domain.
message
events from anywhere. If you have a workaround, that is probably ok. – Hostler