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 Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us

Got an idea? We'll turn it into reality.

Tell us what you're building, what's broken, or what numbers need to move. We'll come back within one business day with a short plan — no long decks, no sales calls without purpose.

Response time: First reply within 1 business day on 100% of messages.
Transparent scope: Fixed proposals with weekly check-ins & live task tracking.
Ship-ready builds: Every engagement leaves behind documented, transferable work.
Long-term partners: Ongoing retainers for support, SEO, ads, and content.