We help IT Professionals succeed at work.
Get Started

How do I transmit data from Javascript to Flash

magman5
magman5 asked
on
578 Views
Last Modified: 2012-05-11
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
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE