postMessage( )


Sends a message to the page's parent.

If a page is embedded within another site, using an HtmlComponent on a Wix site or an iframe on a non-Wix site, you can use the postMessage() function to send a message from the inner site to the outer site.

When the parent site is a Wix site, use the onMessage() function to receive the message on the parent page.

When the parent site is a non-Wix site, use the page's window.onMessage event handler to read the data property of the received MessageEvent to receive the message on the parent page.

Method Declaration
Copy
function postMessage(message: object, target: string): Promise<object>;
Method Parameters
messageMessageRequired

The message to send.


targetstring

The target to send the message to. Must be "parent" or omitted. Defaults to "parent".

Returns
Return Type:Promise<object>
JavaScript
/* * * * * * * * * * * * * * * * * * * * * * * * Code for the inner site to post a message * * * * * * * * * * * * * * * * * * * * * * * */ import { createClient } from "@wix/sdk"; import { site } from "@wix/site"; import { window as wixWindow } from "@wix/site-window"; const wixClient = createClient({ host: site.host(), modules: { wixWindow }, }); // ... await wixClient.wixWindow.postMessage(dataObj); /* * * * * * * * * * * * * * * * * * * * * * * * * * Code for the outer site to receive a message * * * * * * * * * * * * * * * * * * * * * * * * * */ // Assuming the HTML component is an iframe const iframe = document.getElementById("myHtmlComponent"); window.addEventListener("message", (event) => { let message = event.data; });
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?