Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Messages from Parent to IFrames seems to not work #95

Open
shabeesh opened this issue May 19, 2021 · 1 comment
Open

Sending Messages from Parent to IFrames seems to not work #95

shabeesh opened this issue May 19, 2021 · 1 comment

Comments

@shabeesh
Copy link

shabeesh commented May 19, 2021

Hi,

I have a child page running at localhost:8080/apps/child.html, as below.

<!DOCTYPE html>
<script src="https://unpkg.com/[email protected]/dist/post-robot.js"></script>
<div>
    <p id="content">child page</p>
</div>

<script>
 listener=postRobot.on('getUser',{domain: 'http://localhost:8080'}, function(event) {
    console.log(event)
return {
    id:   1234,
    name: 'Zippy the Pinhead',
  };
});
console.log("listener is registered here ")
console.log(listener)
</script>

I have a parent page where the child is included in an iFrame.

<!DOCTYPE html>

<script src="https://unpkg.com/[email protected]/dist/post-robot.js"></script>
<h1> Parent Sending Page</h1>

<iframe src='./Child.html' frameborder="0" id='123'></iframe>
<button onclick=sendMessage()>iFrame Test</button>

<script>
  function sendMessage(){
    let poFrame=document.getElementsByTagName("iframe")[0]
    let poAppSpot=poFrame.contentWindow.parent

    console.log(poFrame)
    console.log(poAppSpot)
  /////
  postRobot.send(
    poAppSpot,'getUser', { id: 1337 } ,  {domain: 'http://localhost:8080'}).
    then(
        function(event) {
          console.log("*** expecting response HERE *** ")
          console.log(event)
          console.log("*** expecting response *** ")
    }).catch(function(err) {

    // Handle any errors that stopped our call from going through

    console.error(err);
});

}
</script>

The scenario is simple, when thebutton testIFrame is clicked, it triggers the sendMessage() method which post the getUser to the Iframe.

    let poFrame=document.getElementsByTagName("iframe")[0]
    let poAppSpot=poFrame.contentWindow.parent // tried with .top as well.

This results in an error No handler found for post message: getUser from http://localhost:8080 in http://localhost:8080/apps/index.html.
Testing in Chrome Version 90.0.4430.212

Do let me know what I am missing as I can't find documentation with regards to sending to iframe.

@ryandoll
Copy link

Instead of let poAppSpot=poFrame.contentWindow.parent just try let poAppSpot=poFrame.contentWindow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants