How to resolve the following error in the mxml program?
The error is
Loading configuration file /opt/flex/frameworks/flex-config.xml
/home/tom-j/programs/flex/html/aa.mxml(17): Error: Access of undefined property addBody.
ExternalInterface.addCallback("addBody", addBody);
Program is
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;
import flash.external.*;
import flash.external.ExternalInterface;
// "javascript function", flash function
ExternalInterface.addCallback("addBody", addBody);
public function addBody():void
{
Alert.show("Got input from JS");
}
]]>
</mx:Script>
</mx:Application>
You should try putting this call into an onCreationComplete event handler:
protected function onCreationComplete(event:FlexEvent):void
{
//just in case to prevent security exceptions:
Security.allowDomain("*.yourdomain.com");
Security.allowDomain("localhost");
ExternalInterface.addCallback("addBody", addBody);
}
The raised error could be because when processing the addCallback
line, there did not exist the addBody
method.
Update calling the addBody
method from javascript
in your html you must have the swf embedded ~like this:
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="your_flash_app" width="1000" height="520"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="YourFlashApp.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#bee3f6" />
<param name="allowScriptAccess" value="always" />
<embed src="YourFlashApp.swf" quality="high" bgcolor="#bee3f6" width="1000" height="520" name="your_flash_app" align="middle" play="true" loop="false"
quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
To call the addBody
method from javascript, first you must locate the flash application there:
//this only works for sure in IE browsers, but there are workarounds
//to deal with the others.
var flashApp = document.getElementById("your_flash_app");
flashApp.addBody();