Atlante Avila
Atlante Avila

Reputation: 1488

How to use window.postmessage to communicate with parent window

I'm creating a react app that I want my users to embed on their websites. This app will be shown in two iframes but I want to only show one iframe at a time. So one part of the app would be a simple button, the second would be a form that pops up once a user has decided to use the app (it's a chat app.)

I have set up a very simple component that displays the button so that it can send a message to the parent website. I'm not sure what I'm doing wrong but when the window.postmessage event is sent, I get the error:

Uncaught DOMException: Blocked a frame with origin "http://localhost:3002" from accessing a cross-origin frame.

Here is the example parent website code:

<h1>Hello! This is some static content</h1>
    <iframe src="http://localhost:3002"
        style="width: 108px; height: 50px; padding: 0px; margin: 10px 20px; position: fixed; bottom: 0px; right: 0px; overflow: visible; opacity: 1; border: 0px; z-index: 999998; transition-duration: 250ms; transition-timing-function: cubic-bezier(0.645, 0.045, 0.355, 1); transition-property: opacity, top, bottom;"
        id="dbhchat"></iframe>
    <h3>Thanks for checking out my blog!</h3>
    <script>
        window.addEventListener('message', event => {
            // IMPORTANT: check the origin of the data! 
            if (event.origin.startsWith('http://localhost:3002')) {
                // The data was sent from your site.
                // Data sent with postMessage is stored in event.data:
                console.log(event.data);
            } else {
                // The data was NOT sent from your site! 
                // Be careful! Do not use it. This else branch is
                // here just for clarity, you usually shouldn't need it.
                return;
            }
        });
    </script>

and my component that sends the message to the parent window:

import React from 'react';
import { IconButton } from '@material-ui/core';
import PhotoCamera from '@material-ui/icons/PhotoCamera';
import './App.css';

function App() {
  const send = () => {
    if (window && window.parent) {
      console.log('we have message sending here', window.parent);
      window.parent.postMessage('message', 'http://localhost:3002', false);
    }
  }
  return (
    <div className="App">
      <header style={{ background: 'red' }} className="App-header">
        <IconButton onClick={send} color="primary" aria-label="upload picture" component="span">
          <PhotoCamera />
        </IconButton>
      </header>
    </div>
  );
}

export default App;

Thanks in advance for any help figuring this out!

Upvotes: 5

Views: 22302

Answers (1)

Atlante Avila
Atlante Avila

Reputation: 1488

I found a bit of a quirky solution to the problem. I don't really know why this works but simply adding e.preventDefault() to the react send function worked fine after that.

const send = (e) => {
    e.preventDefault();
    if (window && window.parent) {
      window.parent.postMessage({message: 'The message is being set up here', hide: 'dbhchat', show: 'dbhchat'}, '*');
    }
}

after adding that, all worked fine. If anyone can provide more details as to why that'd be great!

Upvotes: 6

Related Questions