Jump to content

Child to parent messaging within iframes


walla

Recommended Posts

Hello,
I am trying to postMessage to the parent and can't make it work.
I tried to manipulate it in many ways, here is the last try, that also doesn't work.
Will appreciate any kind of help.
Thanks
This is the receiver:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>    <meta charset="utf-8" />   <title>receiver</title></head><body><iframe id="receiver">   <script type="text/javascript">      window.addEventListener('message', receiveMessage, false);       function receiveMessage(evt)        {           alert("got message: " + evt.data);       }   </script>         <iframe id="sender" src="./sender.html"></iframe></iframe><div>Please wait..</div></body></html>
And here is the sender:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>    <meta charset="utf-8" />    <title>sender</title></head><body><div>sender..</div>    <script type="text/javascript">        var id= "12345667";var o = parent.getElementById('receiver');        o.postMessage(id, '*');    </script></html>

 

Link to comment
Share on other sites

Is the sender.html being loaded correctly (i.e. you can see the text in the body on the page)? are you checking for errors in the console?

 

A couple things:

  1. I'm not sure why you have your script code in receiver in an iFrame though. Just put the javascript in the <head> and the sender iframe in the <body>
  2. With that, you probably want to send postMessage right to parent.

Edited by thescientist
Link to comment
Share on other sites

The content within iframe tags is meant to be displayed if the browser doesn't support iframes. You need to make a separate HTML document to put that script in, which you have to put as the src attribute of the iframe.

  • Like 1
Link to comment
Share on other sites

The content within iframe tags is meant to be displayed if the browser doesn't support iframes. You need to make a separate HTML document to put that script in, which you have to put as the src attribute of the iframe.

Thanks! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...