ramesh en
ramesh en

Reputation: 93

Parent window to iframe messaging not working

I have a window, which is the parent. Then there is an iframe which is inside this window. In the parent window, I am making an ajax call. After I receive data from the call, I need to post the data to the child iframe.

In the child iframe, I have registered the call as given below -

function Mymessage() {
    console.log("Received message from parent window");
}
window.addEventListener("message", Mymessage, false);

In the parent, on success of ajax call, I am sending the data to child iframe as given below -

window.postMessage(mydata, '*');

Problem - I have been debugging, reading about it. But not working. The "Mymessage" function does not get called at all. Why?

When I put debug point on window.postmessage, it hits it. But when I try to enter inside it, it just goes to next line. Is this right?

Upvotes: 0

Views: 1262

Answers (1)

Cuong Pham
Cuong Pham

Reputation: 126

Please try bellow:

window.onload = function() {
    (window.addEventListener && window.addEventListener('message', Mymessage, false) // FF,SA,CH,OP,IE9+
                    || window.attachEvent && window.attachEvent('onmessage', Mymessage)); // IE8 
}

Upvotes: 1

Related Questions