ExternalInterface.addCallback
Copy link to clipboard
Copied
when communicating with flash from javascript is there a special setup that is required for flash.
the function in my actionscript (flash) is never called (the trace is never executed). i have been
searching for examples and help and everything i read looks like this should work.
all i am trying to do is send a text string to flash from javascript.
can anyone help me on this?
thanks
ACTIONSCRIPT 3.0 (code)
i have a flash program with just a dynamic text item named myTxt.
import flash.external.ExternalInterface;
import flash.display.*;
if (ExternalInterface.available) {
trace("external call okay");
ExternalInterface.addCallback("sendTextToAS3", receiveTextFromJS);
}
//Set up Javascript to Actioscript
function receiveTextFromJS(t:String):void {
trace('here');
myTxt.text = t;
}
HTML - JAVASCRIPT (code)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
// get our flash movie object
var flashMovie;
function init() {
if (document.getElementById) {
flashMovie = document.getElementById("flashDIV");
}
}
// wait for the page to fully load before initializing
window.onload = init;
function sendTextToAS3(){
alert ('send');
var Txt = document.getElementById('htmlText').value;
alert ('send ' +Txt);
flash.sendTextFromJS(Txt);
}
var flashvars = {};
var params = {};
params.allowscriptaccess = "always";
params.swliveconnect = "true";
var attributes = { id: "fvars1", name: "fvars1" };
swfobject.embedSWF("fvars1.swf", "flashDIV", "800", "600", "9.0.0", false, flashvars, params, attributes);
</script>
</head>
<body>
<textarea name="htmlText" id="htmlText" cols="50" rows="15"> </textarea><br />
<input type="button" name="sendToFlash" id="sendToFlash" value="Send Text To Flash" onclick="sendTextToAS3();" />
<div id="flashDIV">
<a href="http://www.adobe.com/go/getflashplayer">
<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
</a>
</div>
</body>
</html>
Copy link to clipboard
Copied
with addCallBack() yes, there is special javascript that needs to be used. to be sure:
flash.sendTextFromJS(Txt);
isn't correct. it's possible
flashMovie.sendTextFromJS(Txt);
might work but i doubt it. you should check the flash help files for sample code.
Copy link to clipboard
Copied
thanks kglad. i corrected that and it did not fix.
Copy link to clipboard
Copied
is my code that far off???
Copy link to clipboard
Copied
try:
function sendTextToAS3(){
var Txt = document.getElementById('htmlText').value;
alert ('send ' +Txt);
document.getElementById("fvars1").sendTextFromJS(Txt);
}
Copy link to clipboard
Copied
thanks, really appreciate your input.
did not work. must be something i am
doing that is wrong.
this stuff is not straightforward for me
and its hard to find examples that work.
but i do thank you
Copy link to clipboard
Copied
then try:
function sendTextToAS3(){
var Txt = document.getElementById('htmlText').value;
alert ('send ' +Txt);
document.getElementById("flashDIV").sendTextFromJS(Txt);
}
p.s. actually, assigning two different id's for the same div may be a problem. use one of the other id values but not two different ones.
Copy link to clipboard
Copied
kglad,
thanks for your responses and help.
the code snippets you provided did not work......BUT the
links of where to find some help and examples are
of great use.
thanks for pointing me in the right direction for research.
teremoto
Copy link to clipboard
Copied
you're welcome.

