FlashからJavaScriptにアクセスする方法

ExternalInterface API を使用

HTMLソース

<html>
<head>
<title>wrapper/WrapperBeingCalled.html</title>
</head>
<body scroll='no'>
<script language="JavaScript">
  function changeDocumentTitle(a) {
  window.document.title=a; 
  document.getElementById('title').innerHTML = a;
  return "successful";
}
</script>
<div id="title">Wrapper Being Called</div>
<table width='100%' height='100%' cellspacing='0' cellpadding='0'>
  <tr>
    <td valign='top'>
      <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'   
              codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab' 
              height='200' width='400'>
        <param name='src' value='External.swf'/>
        <param name='flashVars' value=''/>
        <embed name='mySwf' src='External.swf' 
               pluginspage='http://www.adobe.com/go/getflashplayer' height='50%' width='50%' flashVars=''/>
      </object>
    </td>
  </tr>
</table>
</body>
</html>

MXMLソース

<?xml version="1.0"?>
<!-- wrapper/WrapperCaller.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Script>
     import flash.external.*;
  
     public function callWrapper():void {
        var s:String;
        if (ExternalInterface.available) {
           var wrapperFunction:String = "changeDocumentTitle";
           s = ExternalInterface.call(wrapperFunction,ti1.text);
        } else {
           s = "Wrapper not available";
        }
        trace(s); 
     }
  </mx:Script>
  
  <mx:Form>
     <mx:FormItem label="New Title:">
        <mx:TextInput id="ti1"/>
     </mx:FormItem>
  </mx:Form>
  
  <mx:Button label="Change Document Title" click="callWrapper()"/>
</mx:Application>