Category: JS Objects
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Accessing Javascript Function Written Inside an Iframe

Issue

As a web developer, I am having trouble accessing a javascript function inside an iframe from a button onClick event on my main page. I wrote an HTML code with a table and a getCellData() function inside the iframe, but when I call the function from the button, it doesn't work. Unfortunately, I am unable to solve this problem.

Resolution

Instead of trying to directly access the iframe’s JavaScript context, you should use message-based communication between the parent app and the iframe. In Appsmith, this is done using postMessage, which allows the iframe to send messages to the Appsmith app and vice versa in a controlled and supported way.

You can follow the official Appsmith guide here for the correct implementation and examples:
https://docs.appsmith.com/build-apps/how-to-guides/Communicate-Between-an-App-and-Iframe#send-messages-to-appsmith