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.
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:
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:
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:
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().
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: