Lightning Web Component postMessage to iframe

On the "sendData" method the origin variable needed to be preceded with "this" as it wasn't being recognised otherwise

    sendData(){
        let message = 'testing';
        this.template.querySelector('iframe').contentWindow.postMessage(message, this.origin);


    }

Make sure the target window you're/FB is posting messages to is loaded. Most of the time I usually get this error when a hidden iframe I was sending messages to, failed to load.

If this doesn't work change the: origin = 'https://******.visualforce.com'; to 'http://******.visualforce.com';


Possible issue could be your VF page might not have loaded by the time you are posting from lightning component.

Add an event listener to your lightning component, which listens for message . In your VF, you do window.onload() function, where you postmessage to your lightning component with body 'loaded'. When your VF is loaded, it sends out a message, lightning component will receive the message. After you receive the message, it is sure that VF has loaded completely so that you can send message from lightning component