postMessage( )
Sends a message to the page's parent.
Description
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.
Syntax
function postMessage(message: Object, [target: string]): Promise<Object>
postMessage Parameters
NAME
TYPE
DESCRIPTION
The message to send.
The target to send the message to. Must be "parent"
or omitted. Defaults to "parent"
.
Returns
Fulfilled - The data returned from the modal window. Rejected - The error that caused the rejection.
Return Type:
Was this helpful?
1/* * * * * * * * * * * * * * * * * * * * * * *2 * Code for the inner site to post a message *3 * * * * * * * * * * * * * * * * * * * * * * */4import wixWindow from 'wix-window';56// ...78wixWindow.postMessage(dataObj);910/* * * * * * * * * * * * * * * * * * * * * * * * *11 * Code for the outer site to receive a message *12 * * * * * * * * * * * * * * * * * * * * * * * * *13 *14 * $w("#myHtmlComponent").onMessage( (event, $x) => {15 * let message = event.data;16 * } );17 */
1/* * * * * * * * * * * * * * * * * * * * * * *2 * Code for the inner site to post a message *3 * * * * * * * * * * * * * * * * * * * * * * */4import wixWindow from 'wix-window';56// ...78wixWindow.postMessage(dataObj);910/* * * * * * * * * * * * * * * * * * * * * * * * *11 * Code for the outer site to receive a message *12 * * * * * * * * * * * * * * * * * * * * * * * * *13 *14 * <script>15 * window.addEventListener("message", event => {16 * let message = event.data;17 * } );18 * </script>19 */