Note that there are some explanatory texts on larger screens.

plurals
  1. POhtml5 setting onmessage handler for iframe in parent script
    text
    copied!<p>I need to test out a situation where I have a parent window hosting an iframe which will be empty. I need to use the iframe as a proxy between the parent and a cross-domain resource. I've created a simple <a href="http://jsfiddle.net/shelbyz/RJrX7/" rel="nofollow">jsfiddle</a> that should attempt to post a message from the parent to the child iframe.</p> <p>What I try to do in the parent's javascript is have a handler for the child iframe, but for the life of me I cannot figure out the syntax to get it working correctly. I have tried using an addEvent helper to get the event properly added (for IE and everybody else) from examples I've seen in Modern Javascript Develop and Design:</p> <pre><code>addEvent: function(obj, type, fn) { if (obj &amp;&amp; obj.addEventListener) { obj.addEventListener(type, fn, false); } else if (obj &amp;&amp; obj.attachEvent) { obj.attachEvent('on' + type, fn); } } </code></pre> <p>The way I'm currently trying to setup the handler for onmessage is as follows:</p> <pre><code>document.getElementById("frame1").onmessage = function(e) { alert(e.data); }; </code></pre> <p>I have also thought that maybe I need to be accessing the element's contentWindow, but onmessage does not seem to exist.</p> <p>Is there a property or way to access an iframe such that it appears like a window and would have an onmessage that could be handled?</p> <p><strong>Update</strong></p> <p>I can see the value of onmessage set to a function and even alert(...) out the value to see that it is indeed my intended function, but I do not see it called when postMessage is called on the child iframe.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload