IFrameと親ページの間で通信する方法
3116 ワード
親ページから
親ページで
const iframe = document.getElementById('iframe');
iframe.contentWindow.postMessage('some message', '*');
iframeではwindow.onmessage = function(e) {
if (e.data === 'some message') {
alert('It works!');
}
};
からiframe ->親ページ
親ページで
window.onmessage = function(e) {
if (e.data === 'from iframe') {
alert('It works!');
}
};
iframeではwindow.top.postMessage('from iframe', '*')
SourceReference
この問題について(IFrameと親ページの間で通信する方法), 我々は、より多くの情報をここで見つけました https://dev.to/alexadam/how-to-communicate-between-an-iframe-and-the-parent-page-37k7テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol