views:

44

answers:

3

I'm trying to use ExternalInterface.addCallback to allow js to call an as3 method. My code is as follows:

AS:

ExternalInterface.addCallback("sendToActionscript", callFromJavaScript);

function callFromJavaScript():void{ 
circle_mc.gotoAndStop("finish"); 
}

JS:

<button type="button" onclick="callToActionscript()">Switch to square</button> 
<script type="text/javascript"> 
function callToActionscript() { 
flashController = document.getElementById("jstoactest")
flashController.sendToActionscript(); 
} 
</script>

It's not working. What am I doing wrong?

A: 

make sure that AllowScriptAccess is set to "always" or "sameDomain" in embedding the flash and see if that helps. livedocs

shortstick
A: 

I set a isFlashReady flag in JS as FALSE. Then when your SWF is loaded, after Event.ADDED_TO_STAGE is fired, I add ExternalInterface.addCallback and flip isFlashReady flag to TRUE. This prevents a call to SWF before it's ready. You might want to throw alert() in JS functions below to see where it's stuck. Hope this helps.

JS:

var isFlashReady = false;
function thisMovie(movieName)
{
     if (navigator.appName.indexOf("Microsoft") != -1)
     return window[movieName];
}else{
     return document[movieName];
}
function callToActionScript(value)
{
     if(isFlashReady)
     {
          thisMovie("SWFID").sendToActionScript();
     }
}
function flashReady(value)
{
     isFlashReady = true;
}

AS:

if (ExternalInterface.available) {
        try {
                ExternalInterface.addCallback("sendToActionScript", callFromJavaScript);
                flash.external.ExternalInterface.call(flashReady,true);
            } catch (error:SecurityError) {
                trace("A SecurityError occurred: " + error.message + "\n");
            } catch (error:Error) {
                trace("An Error occurred: " + error.message + "\n");
            }
} else {
            trace("External interface is not available for this container.");
}
A: 

Is your SWF file being served from the same Domain as your HTML page? If the domains differ then you will need to use Security.allowDomain to allow the two to communicate.

Also, I've found the easiest way to debug Flash -> JavaScript communication is to use FireBug for Firefox. (sorry I can only post one link!)

JonnyReeves