SamM
SamM

Reputation: 1

Javascript Cross Domain PostMessage not working

I have a page that opens a child window (having iframe) and from new page I am calling Parent function to pass some values from Child to parent window. Child Window Code:

 window.opener.ParentFunction(sendvalues);

Since both are in same domain (example.com)it is working fine for me. Now because of some change I need to move my child window to a new domain (new.example.com). Nor i am getting Premission denied error.

In order to fix this I am using PostMessage.

Child window code:

window.parent.postMessage(sendvalues,"*");

Parent Code:

var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";

var eventer = window[eventMethod];    

var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";


eventer(messageEvent, function (e) {
//for debug    
alert(e.data);

ParentFunction(e.data);

}, false);

This is not working for me I am not getting any value. No error, nothing is happening. The code is not getting executed in eventer method.I am not getting any alert.

Any help is greatly appreciated.

Upvotes: 0

Views: 5392

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

Based on new information, you should be calling:

if the child and iframe are in the same domain

window.parent.opener.postMessage

if the parent and child are in the same domain and the iframe isn't, the iframe may need to call

window.parent.postMessage

and then, the child window should listen and pass on the message to the parent using

window.opener.postMessage

although, you could still try window.parent.opener.postMessage in this case - I'm not about to test everything for you ;)

Upvotes: 1

Related Questions