> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Method name: onMessage(handler: HtmlComponentMessageEventHandler)

# Method package: w

# Method menu location: w --> HtmlComponent --> onMessage

# Method Link: https://dev.wix.com/docs/velo/velo-only-apis/$w/html-component/on-message.md

# Method Description:

Adds an event handler that runs when the HTML Component
 sends a message.

The `onMessage()` function allows your page code to receive messages
 from an HTML Component on your page. When a message is received,
 the specified event handler is executed and the message can be retrieved
 using `event.data`.

 To send a message from your HTML Component, use the postMessage() function
 in the HTML component's code. Generally, you call postMessage()
 from in a function:

     
       function sendReturnMessage(msg) {
         window.parent.postMessage(msg, "http://mysite.com");
       }
     

 When posting a message from in 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()](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage).

 For more information on sending and receiving messages between your page
 and your HTML Component, see [Working with the HTML Component in Wix Code](https://support.wix.com/en/article/working-with-the-html-component-in-wix-code#messaging-1).



# Method Code Examples:

*** Note: do not assume any prop names or enum values other than the ones in the example.

## Receive a message from an HTML Component

```javascript
$w("#myHtmlComponent").onMessage( (event) => {
  let receivedMessage = event.data;
} );
```

## Communicate with an HTML Component

```javascript
$w.onReady(() => {
  // Send message to HTML Component
  $w('#sendButton').onClick(() => {
    $w('#htmlComponent').postMessage('HELLO FROM THE PAGE');
  });

  // Handle messages that the HTML Component sends to the page
  $w('#htmlComponent').onMessage((event) => {
    $w('#message').text = event.data;
  });
});
```

## Send a message to an HTML Component and receive a confirmation

```javascript
/* * * * * * * * * * * * * * * * * * * * * * * * *
 * Paste the following into the HTML Component:  *
 * * * * * * * * * * * * * * * * * * * * * * * * *

<!doctype html>
<html>
<head>

<script type="text/javascript">
function init () {
  // when a message is received from the page code
  window.onmessage = (event) => {
    if (event.data) {
      console.log("HTML Code Element received a message!");
      insertMessage(event.data);
    }
  }
}

// display received message
function insertMessage(msg) {
  document.getElementById('demo').innerHTML = msg;
  sendReturnMessage("Message from the HTML Component!");
}

// send message to the page code
function sendReturnMessage(msg) {
  window.parent.postMessage(msg, "http://mysite.com");
}
</script>

</head>

<body onload="init();" style="background-color:lightgray;">
<h1>HTML Component Test</h1>
<p id="demo">Message will go here</p>
</body>
</html>

 * * * * * * * * * * * * * * * * * * * * * * * * *
 * This is the page code:                        *
 * * * * * * * * * * * * * * * * * * * * * * * * */

$w.onReady(function () {
  // when a message is received from the HTML Component
  $w("#myHtmlComponent").onMessage( (event) => {
    console.log(`Message received by page code: ${event.data}`);
  } );
} );

export function messageSendButton_onClick() {
  // send message to the HTML Component
  $w("#myHtmlComponent").postMessage("Message from page code!");
}

```

---