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>
    window.addEventListener('message', function(event) {
        var iframeMessage =;

Code in the iframe.html file

    var message = 'Your message from iframe to main window';
    window.parent.postMessage(message, "*");
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