ExternalInterface error - _ is not a function

Hi All,

I’m trying to use the External Interface class to communicate between Javascript and Flex so I can create a javascript API to call all my flex functions! I’ve set this up as follows


MXML

I call my javascript setUp function and add a callback to my testing function

private function init():void {

            //Setting up the CallBacks for API methods
            ExternalInterface.addCallback("testIfWorks", testIfWorks);
            
            //Testing the calls made from the API
            if (ExternalInterface.available) {
                ExternalInterface.call("setUp"); 
            }
            else {    
                Alert.show("Feature requires web browser", 'Error',mx.controls.Alert.OK);
            }

        }

Javascript

I keep my js stuff in linechart_api.js. My swf is called main so I have a setUp function as follows:

//Set up a global varibale referencing the SWF and allowing access to the SWF functions
function setUp() {
try{
lineChart = (navigator.appName.indexOf(“Microsoft”)!=-1)?window[“main”]:document[“main”];
alert(“app initialized”);
}catch(e){
alert(“SWF cannot be found”);
}
}

I then call the following testing function

function testAPI(){
try{
var res = lineChart.testIfWorks();
return res;

}catch( e ) {
    var str_msg;
    str_msg += "Name: " + e.name + "

";
str_msg += "File Name: " + e.fileName + "

";
str_msg += "Line Number: " + e.lineNumber + "

";
str_msg += "Message: " + e.message + "

";
str_msg += "Stack: " + e.stack + "
";
alert( str_msg );

}

}


HTML Tester

I add in my javascript API file

<script src=“linechart_api.js” language=“javascript”></script>

and have the following function to call my test

<script language=“JavaScript” type=“text/javascript”>
function test(){
try{
testAPI();
}catch( e ) {
var str_msg;
str_msg += "Name: " + e.name + "

";
str_msg += "File Name: " + e.fileName + "

";
str_msg += "Line Number: " + e.lineNumber + "

";
str_msg += "Message: " + e.message + "

";
str_msg += "Stack: " + e.stack + "
";
alert( str_msg );
}
}
</script>

and I have a button to test this

<button onclick=“test()”>Test</button>

When I click the button I get the following error

lineChart.testAPI is not a function

Can anyone see where I’m going wrong here or how I might get it working? I tried adding a timer to the function of 200milliseconds but the same error keeps coming up? Any help really would be greatly appreciated as this is starting to drive me a bit mental!!!

Thanks,
Derm

I’m thinking this is an issue with the swf not been fully up and running (or not been fully created) before the initialise function is called! Does anyone know how to ensure that the swf is fully loaded before I make any calls on the javascript side?!