postMessage( )
Sends a message to the HTML Component.
Description
The postMessage()
function sends a message from your page code to the
HTML Component.
To receive the message sent from the postMessage()
function in your
HTML Component, create an event handler for the window.onmessage
event
in the component'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.
Often, you define the window.onMessage
event handler in a function
that gets called when the HTML component loads using the body onload
or window.onload
:
Copy Code<script type="text/javascript">window.onmessage = (event) => {if (event.data) {console.log(`HTML Component received a message: ${event.data}`);// additional code here}}</script>
For more information on sending and receiving messages between your page and your HTML Component, see Working with the HTML Component in Wix Code.
Notes:
An HTML Component needs to load before you can send it messages using the
postMessage()
function.Usually an HTML Component finishes loading a short time after the page it is on finishes loading. So if you call
postMessage()
inside the page’sonReady()
event handler, the HTML Component might not be ready yet. To callpostMessage()
as soon as possible after a page loads, send a message from the HTML Component to the page code as when the HTML Component is loaded and then callpostMessage()
upon receipt of that message.Also, an HTML Component can send messages to the page it is on before the HTML Component itself has loaded completely. For example, this can happen if the HTML Component’s body, or one of its downloaded images, is large. To make sure the HTML component is loaded, send messages only after listening for an onLoad event in the HTML component.
Syntax
function postMessage(message: string | number | boolean | Object | Array<*>): void
postMessage Parameters
NAME
TYPE
DESCRIPTION
The message to send to the HTML Component.
Returns
This function does not return anything.
Return Type:
Related Content:
Was this helpful?
1$w("#myHtmlComponent").postMessage("Message from page code!");
This example shows how to send information from the page to the HTML Component and from the HTML Component to the page. You can test out the code in our example template.
1$w.onReady(() => {2 // Send message to HTML Component3 $w('#sendButton').onClick(() => {4 $w('#htmlComponent').postMessage('HELLO FROM THE PAGE');5 });67 // Handle messages that the HTML Component sends to the page8 $w('#htmlComponent').onMessage((event) => {9 $w('#message').text = event.data;10 });11});
This example assumes you have created a page with an HTML Component
whose ID is myHtmlComponent
and a button whose onClick event handler
is set to messageSendButton_onClick
.
When the user clicks the button, a message is sent to the HTML Code element. Code inside the element receives and displays the message. Then it sends a message back to the page code. The page code receives and logs the message.
1/* * * * * * * * * * * * * * * * * * * * * * * * *2 * Paste the following into the HTML Component: *3 * * * * * * * * * * * * * * * * * * * * * * * * *45<!doctype html>6<html>7<head>89<script type="text/javascript">10function init () {11 // when a message is received from the page code12 window.onmessage = (event) => {13 if (event.data) {14 console.log("HTML Code Element received a message!");15 insertMessage(event.data);16 }17 }18}1920// display received message21function insertMessage(msg) {22 document.getElementById('demo').innerHTML = msg;23 sendReturnMessage("Message from the HTML Component!");24}2526// send message to the page code27function sendReturnMessage(msg) {28 window.parent.postMessage(msg, "http://mysite.com");29}30</script>3132</head>3334<body onload="init();" style="background-color:lightgray;">35<h1>HTML Component Test</h1>36<p id="demo">Message will go here</p>37</body>38</html>3940 * * * * * * * * * * * * * * * * * * * * * * * * *41 * This is the page code: *42 * * * * * * * * * * * * * * * * * * * * * * * * */4344$w.onReady(function () {45 // when a message is received from the HTML Component46 $w("#myHtmlComponent").onMessage( (event) => {47 console.log(`Message received by page code: ${event.data}`);48 } );49} );5051export function messageSendButton_onClick() {52 // send message to the HTML Component53 $w("#myHtmlComponent").postMessage("Message from page code!");54}55