1 Reply Latest reply on Mar 10, 2008 7:11 AM by malcolm.j

    Flex and Javascript

    malcolm.j
      Hi all

      Newbie here, trying to learn Flex :)

      I'm having some problems trying to call a JavaScript function from Flex, and would appreciate any help.
      I've written some test cases to try to illustrate the problem....

      JAVASCRIPT:
      function testReturn(name) {
      return "Hello "+name;
      }
      function testAlert(name) {
      alert("Hello "+name);
      }


      FLEX:
      public function test( ):void {
      // Test 1
      var s:String = ExternalInterface.call("testReturn", "Tom");
      trace(s); // OUTPUT: Hello Tom

      // Test 2
      s = ExternalInterface.call("testReturn('Dick')");
      trace(s); // OUTPUT: null

      // Test 3
      ExternalInterface.call("testAlert('Harry')"); // alert box displays correctly
      }


      PROBLEM:
      No value is returned in the 2nd test.


      I'd like to be able to call the JS the 2nd way (ie. passing in the function name and parameters as one String) because i need a generic Flex function which can accept any JS passed is, eg:
      public function callJS( myJS:String ):Object{
      return ExternalInterface.call( myJS );
      }

      where myJS could be:
      "complexFn( 'hello', 'world', 1, 2, 3)" or
      "simpleFn( 1 )"

      The alternative would be for my function to parse myJS, and work out the parameters...but then i don't know how to pass these into ExternalInterface.call(). Is it possible to determine the length of the argument list at runtime, and pass these in?

      Any help would be appreciated.

      Regards,

      Malcolm