admin管理员组

文章数量:1287647

I have seen from this documentation: , 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.

I have seen from this documentation: https://developer.mozilla/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.

Share Improve this question edited Jun 11, 2017 at 18:53 User asked Jun 11, 2017 at 17:18 UserUser 1942 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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]);

I would consider using easyXDM

EasyXDM WebSite

本文标签: javascriptSend a message from an iframe on the main pageStack Overflow