回到主页

通过postMessage实现iframe通信

· 前端开发

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;
                    } 

参考资料