title: How do I use Flex's ExternalInterface with IFrames?
body: <p>I have a simple html doc with a flex app and an iframe side-by-side (using a 1x2 table).</p>
  
  <p>There is a button in the flex app that, when clicked, sets the "src" property of the iframe to a URL by using the ExnternalInterface.call method to call a javascript function "doNavClick(url)".</p>
  
  <p>Or that's the idea.</p>
  
  <p>When I comment out the iframe object declaration in the html, the call to doNavClick works (I pop up an alert to test)....yet when the iframe declaration is present, it doesn't even receive the call. It's as if the iframe has somehow broken the call dispatching mechanism.</p>
  
  <p>Is there a workaraound for this?</p>
  
  <p>The problematic code is as follows:</p>
  
  <p>--- Main.mxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  paddingLeft="0" paddingRight="0"
                  paddingTop="0" paddingBottom="0"
  &gt;
  
    &lt;mx:Script&gt;
      &lt;![CDATA[
  
        import flash.events.MouseEvent;
  
        private function doNavClick(event:MouseEvent):void
        {
          if (ExternalInterface.available)
          {
            var url:String = "http://www.google.com"
            ExternalInterface.call("doNavClick", url); // Problematic with iframes
          }
          else
          {
            mx.controls.Alert.show("External interface not available");
          }
        }
  
      ]]&gt;
    &lt;/mx:Script&gt;
  
    &lt;mx:VBox width="100%" height="100%"&gt;
      &lt;mx:Button label="Google" click="doNavClick(event)"/&gt;
    &lt;/mx:VBox&gt;
  
  &lt;/mx:Application&gt;
  </code></pre>
  
  <hr>
  
  <p>...and the html/JavaScript code containing the swf app:</p>
  
  <p>--- index.html</p>
  
  <pre><code>    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"&gt;
      &lt;head&gt;
          &lt;title&gt;My SWF Project&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
          &lt;script type="text/javascript" src="swfobject.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              swfobject.registerObject("mySWFProj", "9.0.0");
          &lt;/script&gt;
      &lt;style&gt;
        html, body, div { height: 99%; }
      &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;table border="1" width="100%" height="100%"&gt;
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;td style="width: 25%; height: 100%"&gt;
              &lt;div style="width: 100%; height: 100%"&gt;
                &lt;object id="mySWFProj" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%"&gt;
                  &lt;param name="allowscriptaccess" value="always" /&gt;
                  &lt;param name="allowfullscreen" value="true" /&gt;
                  &lt;!--[if !IE]&gt;--&gt;
                  &lt;object type="application/x-shockwave-flash" data="mySWFProj.swf" width="100%" height="100%"&gt;
                    &lt;param name="allowscriptaccess" value="always" /&gt;
                    &lt;param name="allowfullscreen" value="true" /&gt;
                  &lt;!--&lt;![endif]--&gt;
                    &lt;a href="http://www.adobe.com/go/getflashplayer"&gt;
                      &lt;img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /&gt;
                    &lt;/a&gt;
                  &lt;!--[if !IE]&gt;--&gt;
                  &lt;/object&gt;
                  &lt;!--&lt;![endif]--&gt;
                &lt;/object&gt;
              &lt;/div&gt;
            &lt;/td&gt;
            &lt;td style="width: 75%; height: 100%"&gt;
              &lt;div style="width: 100%; height: 100%"&gt;
                &lt;!-- &lt;iframe id="htmlFrame" width="100%" height="100%" src="http://www.amazon.com"&gt; --&gt;
              &lt;/div&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
  
          &lt;script type="text/javascript"&gt;
  
        function doNavClick(url)
        {
          alert(url);
          //document.getElementById('htmlFrame').src = url;
        }
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p>This code will simply pop up a browser alert with the google url in it. If you uncomment the iframe declaration (with id "htmlFrame") and reload the page, the alert doesn't show, in fact, the JavaScript doNavClick(url) never gets called.</p>
  
  <p>UPDATE: Interesting....when I examine the DOM of index.html (I use firebug for this) the body usually has two child nodes - table &amp; script. When the iframe declaration is present, the script child disappears...which explains why my ExternalInterface.call never goes anywhere. Why does the inclusion of an iframe cause my script object to be removed?</p>
  
  <p>UPDATE2 : Moving the doNavScript(url) script into the "head" of the document fixes this problem. It is as if the iframe declaration prevents any other children being part of the body...I'm not saying that this is what is actually happening, only that this appears to be the symptom.</p>