How to send data from parent to iframe

35 views Asked by At

I have a problem. Earlier I was having a hard time sending a postMessage from the iframe to the parent node. Fortunately, I solved it because somehow window.parent.postMessage didn't work but top.postMessage did work. Now I'm having the opposite problem. I want to postMessage from the parent to the iframe doing myIframe.contentWindow.postMessage but it doesn't work. Is there something else I could try?

I'm testing in localhost, one is at localhost:3000 and the other at localhost:9001

1

There are 1 answers

0
Anthony Nguyen On
const iframe = document.getElementById("myFrame");
const iframeElement = iframe.contentWindow.document.getElementsByTagName("H1")[0];
// Do something with `iframeElement`

Reference: https://www.w3schools.com/howto/howto_js_element_iframe.asp