How do I transmit data from Javascript to Flash

magman5
magman5 used Ask the Experts™
on
I have used code from online resources and can successfully transfer information from Flash to Javascript. For some reason I am unable to call Flash functions from javascript.

The flash file is compiled using Actionscript 3.0 and Flashplayer 9. I am using Firefox 4.0.1 and Firebug 1.7.0 to debug javascript.

When I click the link to send data to flash, I get the Firebug error message:
flashmovie.sendTextFromHtml is not a function

Open in new window


My Flash code is:
import flash.external.ExternalInterface; 
import flash.events.Event; 
function getTextFromJavaScript(str:String):void 
{  
   received_ti.text = "From JavaScript: " + str; 
} 
ExternalInterface.addCallback("sendTextToFlash", getTextFromJavaScript); 
function clickSend(event:Event):void 
{  
   var jsArgument:String = sending_ti.text;  
   var result:Object = ExternalInterface.call("getTextFromFlash", jsArgument);  
   received_ti.text = "Returned: " + result; 
} 
send_button.addEventListener("click", clickSend); 

Open in new window


My Javascript code is:
function getFlashMovie(movieName) 
{
   var isIE = navigator.appName.indexOf("Microsoft") != -1;
   return (isIE) ? window[movieName] : document[movieName];  
}
function formSend(txt) 
{   
   flashmovie = getFlashMovie("swf/compass");
   flashmovie.sendTextFromHtml(txt); 
   console.log("sendTextFromHtml("+txt+")");
}

function getTextFromFlash(str) 
{   
   console.log("getTextFromFlash(" + str+")");
   return str + " received";  
}

Open in new window


The flash file is embedded into html as follows:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="260" height="280" id="swf/compass" align="middle">
<param name="movie" value="swf/compass.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="play" value="true" />
<param name="loop" value="true" />
<param name="wmode" value="transparent" />
<param name="scale" value="showall" />
<param name="menu" value="true" />

<param name="devicefont" value="false" />
<param name="salign" value="" />
<param name="allowScriptAccess" value="always" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="swf/compass.swf" width="260" height="280">
<param name="movie" value="swf/compass.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="play" value="true" />

<param name="loop" value="true" />
<param name="wmode" value="transparent" />
<param name="scale" value="showall" />
<param name="menu" value="true" />
<param name="devicefont" value="false" />
<param name="salign" value="" />
<param name="allowScriptAccess" value="always" />
<!--<![endif]-->
<a href="http://www.adobe.com/go/getflash">

<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
</a>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>

Open in new window


Does anyone have a clue why my "sendTextFromHtml" function cannot be found by javascript?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Got it working.
Use HTML EMBED tags instead of OBJECT tags generated by default by Adobe Flash.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial