Skip to main content
Inspiring
December 13, 2011
Question

ExternalInterface.addCallback

  • December 13, 2011
  • 1 reply
  • 1647 views

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>

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 13, 2011

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.

teremotoAuthor
Inspiring
December 13, 2011

thanks kglad.  i corrected that and it did not fix.

teremotoAuthor
Inspiring
December 13, 2011

is my code that far off???