
If you control both, it's much easier and this example will cover such use-case. Here's how I used postMessage to get the height and width of a document in an iFrame. Javascript now allows cross-document communication thanks to the postMessage function. data that is sent over those messages needs to be serialized it's tricky to start communication because most of the time your aren't sure what loaded first: main window or an iframe. To javascript, iFrames are typically black boxes. From that obtain the origin > const pOrigin. Implemented solution: Capture the click event > onclick'closeParentIframe (event) '. Response of ICoreWebView2Frame2::PostWebMessageAsJson is always S_OK, even if the message does not receive the iframe. main window and an iframe can exchange data using postMessages. There are times when you want to enable communication for an IFRAME that contains content on a different domain. Thank You - postMessage doesn't just turn off all cross-origin safeguards. ICoreWebView2Frame2::ExecuteScript always works even if the prior call to ICoreWebView2Frame2::PostWebMessageAsJson fails to receive the message. Then I tried to use ICoreWebView2Frame2::ExecuteScript right after ICoreWebView2Frame2::PostWebMessageAsJson just to figure out whether a test script can be executed in the iframe. Sometimes it works, but most of the time not. But the iframe does not always receives the reply from the host app, but the the host app sends the message back. The host app always receives the message from the iframe.
IFRAME POSTMESSAGE CODE
This is really weird and currently I do not see a reason for this and I also can not debug this because I do not have access to the internal code to see what happens here. This works sometimes and after a few seconds and retries I get the response. I've tested a bit around and I tried to send the command in an interval until I receive a response. I say often because sometimes it works and sometimes it does not work. But if we change the source of the iframe to load a new version for a new command, then receiving the responses often does not work. This results in a more seamless navigation experience where you can quickly switch between dashboards without reloading the page. The host app processes the commands and sends the result back to the iframe via ICoreWebView2Frame2::PostWebMessageAsJson. When embedding SharpTools.io dashboards within in an iframe, you can use the postMessage API on modern browsers to perform client-side navigation of your embedded SharpTools.io dashboards. The iframe sends commands to the host app via.

We use ICoreWebView2Frame2::add_WebMessageReceived to receive messages from an iframe. We have a problem with PostWebMessageAsJson to an Iframe.
