3 Replies Latest reply on Oct 7, 2007 7:16 AM by Calculon101

    Flex to Javascript return value problem

    Calculon101
      Hi,

      I'm a new Flex convert and I'm having a problem with the ExternalInterface api. I've basically got it all working except that the return value from Javascript isn't getting returned, unless ,I have an alert immediately before the return statement.

      example:
      Flex function:
      if (ExternalInterface.available)
      {
      var num:int = ExternalInterface.call("jsAdd", 2,4);
      }

      Javascript:
      function testAdd(a, b)
      {
      // alert("Why does it work now?"); // SO, VALUE ONLY GETS RETURNED IF THE ALERT FIRES FIRST!
      return (a + b);
      }

      I have the callback functions working fine for Javascript to call my Flex functions so is not a security or timing issue. So, in this case, why does the return value only get passed back to the Flex function when the Javascript alert call is made first? Oddly, if I have two alert() calls before the return statement instead of one again the return value is not passed back! Behaviour is the same in IE7 and Firefox 2 and whether hosted on web server or locally. Hair pulling out time, so if anyone can clue me in to what's going wrong here I'd greatly appreciate it.
        • 1. Re: Flex to Javascript return value problem
          chris.huston.t10 Level 3
          Your ExternalInterface call function needs to match the name of your Javascript function. You are calling "jsAdd", but your javascript is "testAdd". Here is a test app that is working for me with IE 7, Flex3 beta 2:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
          <![CDATA[
          private function addNumbers():void {
          if (ExternalInterface.available) {
          var sum:int = ExternalInterface.call("testAdd",2,4);
          trace(sum);
          }
          }
          ]]>
          </mx:Script>
          <mx:Button x="135" y="53" label="add" click="addNumbers()"/>
          </mx:Application>

          Javascript:
          <script language="JavaScript" type="text/javascript">
          function testAdd(a, b)
          {
          return (a + b);
          }
          </script>

          Vygo
          • 2. Re: Flex to Javascript return value problem
            Calculon101 Level 1
            Thanks for the reply.
            Sorry, the function name missmatch was just an error in translation, they were supposed to be the same, as I mentioned the javascript was being called as the alert was firing but the return value was not getting passed back unless the alert was present.

            But, I've tried your code and it works! It's virtually identical to what I'm using, in fact I haven't changed the html/javascript container at all so I've still got a puzzle with the odd behaviour as mentioned. Oh well, at least I know it works in principle, I'll track it down now.

            Many thanks for your help
            • 3. Flex to Javascript return value problem
              Calculon101 Level 1
              Quick follow up. I've found the cause of the fault but I don't yet understand why. Within a function declared prior to my javascript call function I have this line of code;

              default xml namespace = "urn:ebay:apis:eBLBaseComponents";

              removing this cures the problem, or moving this function to below my javascript calling function declaration also fixes it. According to the manual the scope of a default xml namespace declaration is just within the function block.

              Anyone know why this should cause the problem?