Send message from iframe on the main page

I saw from this documentation: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , a way to correctly pass data to an iframe. But now I want to send an answer:

//from main page
myIframe.contentWindow.postMessage('send me a response', '*');
//from iframe of main page
window.addEventListener("message", receiveMessage, false);
    function receiveMessage(event){
          alert(event.data);//the value of message
          //now i need to send an answer 'this is a response'
    }
}

How to send a response to the main page from iframe? I really need this answer.

Edit:

Ok, I found ty solution in general.

+4
source share
2 answers

I would consider using easyXDM

EasyXDM WebSite

+2
source

You have access to the parent window in the global window.parent.

, , postMessage. - :

var parent = window.parent;
parent.postMessage("some message");

.

- window.parent.postMessage() : otherWindow.postMessage(message, targetOrigin, [transfer]);

+2

Source: https://habr.com/ru/post/1678993/


All Articles