I am using postMessage to share data between https://www.example.com (Which I'll call mainSite) and https://subdomain.example.com (which I'll call subSite)
subSite has an iframe to mainSite,
subSite's code looks something like:
//set the src of the iframe
$("#main-site-iframe").attr("src", "https://www.example.com");
//wait for the main site page to open
$("#main-site-iframe")[0].onload = () => {
//my page posts a message to their site
$("#my-iframe")[0].contentWindow.postMessage("messagePlox", "https://www.example.com");
};
//wait for the iframe to message back
window.addEventListener('message', iframeResponse, false);
function iframeResponse(e) {
//make sure the request is from the correct site
if(e.origin == 'https://www.example.com')
{
//Got the data
console.log(e.data);
}
}
mainSite looks like this:
//listen for the subdomain to make a request
window.addEventListener('message', subdomainRequest, false);
function subdomainRequest(e) {
//make sure the request is from the correct subdomain
if(e.origin == 'https://subdomain.example.com')
{
//respond with the data
e.source.postMessage("We got you", e.origin);
}
}
The problem with the above is that it works in every browser EXCEPT Safari which refuses it and says:
Blocked a frame with origin "https://www.example.com" from accessing a frame with origin "https://subdomain.example.com". Protocols, domains, and ports must match.
Which seems to imply the response back is breaking since mainSite is trying to "access" subSite.
Does anyone know why this would only happen in Safari and not in Firefox or Chrome?