Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a message from an iframe on the main page

I have seen from this documentation: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , the way to pass a data correctly 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 do I send an answer to the main page from the iframe? I need really of this answer.

Edit:

Ok i found the solution ty at all.

like image 466
User Avatar asked Jun 11 '17 17:06

User


2 Answers

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

I believe it is as easy as using this object's method at this point to postMessage. So something like:

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

A full example can be found here.

The gist is the window.parent.postMessage() function takes the following arguments: otherWindow.postMessage(message, targetOrigin, [transfer]);

like image 170
maxwell Avatar answered Oct 02 '22 01:10

maxwell


I would consider using easyXDM

EasyXDM WebSite

like image 27
Sampgun Avatar answered Oct 01 '22 23:10

Sampgun