jeudi 28 février 2019

postMessage Crossdomain Problems in iFrame https:// and https://www

I implemented a Cross-Domain IFrame. On my Main Page i'm using this function:

window.addEventListener("message", receiveMessage, false);

    function receiveMessage(event)
    {
      var origin = event.origin || event.originalEvent.origin; 

      if (origin !== "https://www.domain.xy" && origin !== "https://domain.xy")
        return;

      $('#gbook').height(event.data + 30);
    }

On the Page that should be used inside the IFrame I implemented the following Code:

var height= getAbsoluteHeight('body');
parent.postMessage(
       height, 
       "https://www.domain.xy"
);

Now when I'm opening my Page with the URL "https://www.domain.xy" everything works fine. But as soon as I try to open it by "https://domain.xy" (without the "www.") the Browser throws an exception:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://www.domain.xy') does not match the recipient window's origin ('https://domain.xy').

I already made it work with a workaround by posting two Messages (one Message to "https://domain.xy" and one Message to "https://www.domain.xy"). The result of this is the same Error but at least it works.

Is there a proper solution for this?




Aucun commentaire:

Enregistrer un commentaire