Sends a message to a 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, call this method to send a message from the inner site to the outer site.
When the parent site is a Wix site, call onMessage()
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.
function postMessage(message: object, target: string): Promise<object>;
The message to send.
The target to send the message to. Must be "parent"
or omitted. Default: "parent"
.
/* * * * * * * * * * * * * * * * * * * * * * *
* Code for the inner site to post a message *
* * * * * * * * * * * * * * * * * * * * * * */
import wixWindowFrontend from "wix-window-frontend";
// ...
wixWindowFrontend.postMessage(dataObj);
/* * * * * * * * * * * * * * * * * * * * * * * * *
* Code for the outer site to receive a message *
* * * * * * * * * * * * * * * * * * * * * * * * *
*
* $w("#myHtmlComponent").onMessage( (event, $x) => {
* let message = event.data;
* } );
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.