I'm establishing cross-domain communication with JS's postMessage method. An embedded iframe is sending the message and the parent window is receiving it. The issue is that even after checking for origin in receiveMessage (which reduced the number of messages received by a large amount), I'm still receiving two messages, one that I'm posting, and another which was written quite some time ago for a different purpose. So, I can't really modify this other (unwanted) message's postMessage method. Is there a way in the postMessage method or receiveMessage, for that matter, which can help in identifying which one is mine? Maybe some extra parameter or configuration I'm missing here?
Code for postMessage (in the embedded iframe):
window.parent.postMessage("Hello world!", "*");
Code for receiveMessage (in the parent window):
window.addEventListener("message", receiveMessage, false);
function receiveMessage(e) {
var reliableHost = "://" + "<%= Site.current_site.internal_admin_host %>";
var secureHost = "https" + reliableHost;
var notSecureHost = "http" + reliableHost;
if (e.origin.indexOf(secureHost) != -1 || e.origin.indexOf(notSecureHost) != -1) {
var data = e.data;
console.log(data);
console.log(e.source);
// filter the other event
}
}