as3 ExternalInterface.addCallback is not working right

shibbydoo picture shibbydoo · Nov 19, 2008 · Viewed 19.8k times · Source

I was trying to access swf from javascript, so this example in livedocs is what I'm trying to modify. http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#includeExamplesSummary

However,it is not working correctly for some reason. The problem I'm encountering is that it does not work in Safari and in Firefox, it only works if I put an alert in the function before javascript pass the value to swf. (seems like it needs some time) I also tried to set a timer in as3, but timer doesn't work, only alert in js helps.

All I wanted to do is use js to tell the swf file to play ep1.swf. Here's my js code:

document.observe('dom:loaded', function() {
    $('episode1').observe('click', function() {
        var params = {wmode : "transparent", allowScriptAccess:"always", movie:"header"};
        swfobject.embedSWF("swf/float.swf", "header", "100%", "100%", "9.0.0","expressInstall.swf", "", params, "");
        sendToActionScript("ep1.swf");
    });
})
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        //alert("aaa")
        return document[movieName];
    }
}
function sendToActionScript(value) {     
    thisMovie('header').sendToActionScript(value);       
}

Here's my as3 code:

private function receivedFromJavaScript(value:String):void {

    loader.load(new URLRequest(value));

}

I've been trying for a really long time, does anyone know how to fix this? Thanks.

Answer

jcoder picture jcoder · Nov 21, 2008

The problem is that the SWF file isn't fully loaded by the time you try to call it. The flash player is probably loaded but it takes a while to load and initialise the swf file.

What you need to do is make a call from the SWF file to a javascript function when it's loaded and put your javascript there rather than in the page loaded handler that you seem to be doing now. That way you know that your flash application is properly initialized by then. The ExternalInterface class you are using has methods to let you call back into the javascript.