> For the complete documentation index, see [llms.txt](https://docs.uibakery.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uibakery.io/how-tos/custom-code/communicating-through-the-iframe-component.md).

# Communicating with external sites via Iframe

Let's say you have a website or application embedded in the UI Bakery Iframe component and you need to communicate with it - send or receive data. You can easily do this using the *JavaScript* `postMessage` *function*.

Follow the instruction below for details:

1. Give your Iframe component a *unique name* to send the data. \
   This name will be added as a component class allowing you to query it with JavaScript.
2. Create a **JavaScript Code** action and specify the following code to query the Iframe and send messages to it:

```javascript
const iframe = document.querySelector('.uniqueName > iframe').contentWindow;
iframe.postMessage('message', { foo: 'bar' })
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FPyHNT3g9f3NhnkCXwEO1%2FCleanShot%202025-02-18%20at%2015.35.33%402x-min-min.png?alt=media&amp;token=da84685c-01ab-4a65-b3a3-8743ad0e8e9c" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Depending on the receiving app configuration, `postMessage` may require [additional setup](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#security_concerns).
{% endhint %}

***

Similarly, you can also *subscribe to events from the Iframe* via a **JavaScript Code** action step with the following code:

```javascript
const iframe = document.querySelector('.uniqueName > iframe').contentWindow;
iframe.on('message', (data) => {
  console.log(data);
});
```

And you need to assign this action to the *On Page Load* trigger to bind the listener to the new messages when the app loads.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.uibakery.io/how-tos/custom-code/communicating-through-the-iframe-component.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
