0

On Sender side (localhost:3000):

 const handleExportJSON = () => {
    let win = window.open("http://localhost:3001");
    win.postMessage("Hello there", "http://localhost:3001");
  };

Receiving Side (localhost:3001):

  componentDidMount() {
    window.addEventListener("message", this.receiveMessage, false);
  }

    receiveMessage = event => {
        console.log("Event is", event);
        // if (event.origin !== "http://im-amir/github.io/origin1") return;
    };

    render(){
        return
    ....
}

No data is logged on receiver side. Even the event parameter is not logged. I have also tried "*" in postMessage as a second parameter.

Any help will be appreciated.

Muhammad Amir
  • 593
  • 2
  • 7
  • 21
  • Try adding a `window.addEventListener('message', msg => console.log(msg), false)` somewhere near the absolute top of your script. My guess is the message is posting before `addEventListener` is setup – Blue Nov 26 '19 at 14:12
  • Additionally, try adding it in an onload callback: https://stackoverflow.com/questions/2797560/set-a-callback-function-to-a-new-window-in-javascript – Blue Nov 26 '19 at 14:16
  • @FrankerZ By top of script, what you mean. Sorry, I can't understand "top of script", as I am using react. – Muhammad Amir Nov 26 '19 at 15:15

0 Answers0