I'm working with 2 localdomains, localhost
and domain1
to test a postMessaging
system using iFrames from both domains.
I can correctly identify the respective window
element (on both domains) to send message to and receive replies. My problem is, that the 2nd parameter sent in my postMessage (the URL I'm sending the message from) is not accepted by the listener on the respective other domain. Only if I declare the sender to be "*"
, my messaging works.
This does not work sending a message to the foreign domain:
// targetWindow = window of foreign domain
targetWindow.postMessage({
"foo": "bar"
}, window.location.href);
This works:
// targetWindow = window of foreign domain
targetWindow.postMessage({
"foo": "bar"
}, "*");
Same for replies back to the sending domain (using window.top
). This does not work:
window.top.postMessage({
"baz": "bam"
}, window.location.href.split("?")[0]);
while this does:
window.top.postMessage({
"baz": "bam"
}, "*");
Question:
Why is this so? I thought I had to supply the sending-url as 2nd parameter to enable authentication. If so, why do my events not trigger? Is the reason me working on localhost/domain1?
Thanks for help!