·
iframe 子元素和父窗口,通过postMessage实现通信
通过调用postMessage向父节点抽取
window.addEventListener('message', function(evt) {
if (evt.data === 'gamedata') {
window.parent.postMessage(strucGame(), '*');
}
}, false);
通过调用postMessage来获取result数据
if (waitForInit) {
window.addEventListener('message',function (e) {
const result = e.data;
dispatch({
type: 'updateQuestionContent',
content: result,
});
}, false);
waitForInit = false;
}
参考资料
- https://robertnyman.com/html5/postMessage/postMessage.html
- http://www.cnblogs.com/dolphinX/p/3464056.html
- https://social.msdn.microsoft.com/Forums/en-US/23efd8a3-a377-4060-9e85-48596a514f12/sending-message-from-iframe-to-main-window?forum=winappswithhtml5
- https://stackoverflow.com/questions/8822907/html5-cross-browser-iframe-postmessage-child-to-parent
