1 Reply Latest reply on Jul 10, 2008 2:26 AM by derm116

    ExternalInterface Error

    derm116
      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 + "\n\n";
      str_msg += "File Name: " + e.fileName + "\n\n";
      str_msg += "Line Number: " + e.lineNumber + "\n\n";
      str_msg += "Message: " + e.message + "\n\n";
      str_msg += "Stack: " + e.stack + "\n";
      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 + "\n\n";
      str_msg += "File Name: " + e.fileName + "\n\n";
      str_msg += "Line Number: " + e.lineNumber + "\n\n";
      str_msg += "Message: " + e.message + "\n\n";
      str_msg += "Stack: " + e.stack + "\n";
      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
        • 1. Re: ExternalInterface Error
          derm116 Level 1
          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?!