Messaging Between a Site Page and an HTML iFrame Element

You can use code to send and receive messages between your page and your HTML iFrame element. You can send and receive data as any valid JavaScript type.

Sending a Message from Page Code to an HTML Element

You can send a message from your page to an HTML element using the HTML element's postMessage() function.

For example, if your page contains an HTML element with the ID myHtmlElement:

Copy

Receiving a Message from Page Code in an HTML Element

You can receive a message in your HTML element by creating an event handler for the window.onmessage event in the element's code. You create the event handler within an HTML <script> tag. You get the received data by getting the data property of the event handler's event parameter.

For example, in your HTML element's HTML Code:

Copy

Sending a Message from an HTML Element to Page Code

You can send a message from your HTML element using the postMessage() function in the element's code. Generally, you will be calling postMessage() from within a function.

For example, in your HTML element's HTML Code:

Copy

Important: When posting a message from within your HTML Component, you should specify your site's URL as the targetOrigin. If you use "*" instead, your message can be intercepted by a malicious site. To learn more, see Window.postMessage().

Receiving a Message from an HTML Element in Page Code

You can receive a message in your page code using the HTML element's onMessage() function to bind an event handler. You get the received data by getting the data property of the event handler's event parameter.

For example, if your page contains an HTML element with the ID myHtmlElement:

Copy
Did this help?