JavaScript   

Send data from iframe to main window using JavaScript

We can easily send data from the iframe to the main window using the postMessage method in JavaScript.

Code in the main window

<iframe src="iframe.html">  </iframe>
<script>
    window.addEventListener('message', function(event) {
        var iframeMessage = event.data;
        console.log(iframeMessage);
    });
</script>

Code in the iframe.html file

<script>
    var message = 'Your message from iframe to main window';
    window.parent.postMessage(message, "*");
</script>
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us