Link to home
Start Free TrialLog in
Avatar of philip mcclelland
philip mcclellandFlag for Australia

asked on

How do I transmit data from Javascript to Flash

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?
ASKER CERTIFIED SOLUTION
Avatar of philip mcclelland
philip mcclelland
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial