3 Replies Latest reply on Jun 29, 2007 11:59 AM by Prajakta_Godbole

    Flex Javascript interaction

      In my application, the Flex Javascript interaction works correctly only when I have a "alert" pop-up!!! I do not want the "alert" in my final application.

      Here is my code snippet:

      Javascript code:
      <script language = "JavaScript" type="text/JavaScript" charset="ISO_8859-1">

      var jsReady = false;
      function isReady()
      return jsReady; //to set up communication with Flex

      function pageInit()
      // Record that JavaScript is ready to go.
      jsReady = true;
      var newTerm="Flex"; //This is the input variable, right now its hard-coded
      alert(newTerm);//this alert causes trhe problem, I get to see the desired output with alert, but without alert, I get "Error on Page"
      getMyApp("test").myFlexFunction(newTerm);//call to Flex function


      function getMyApp(appName) //Function to find the type of our browser
      if (navigator.appName.indexOf ("Microsoft") !=-1)
      return window[appName];
      } else {
      return document[appName];


      pageInit() is called onLoad in body tag==> possible cause of problem
        • 1. Re: Flex Javascript interaction
          ntsiii Level 3
          Post the Flex ExternalInterface callback code.

          • 2. Re: Flex Javascript interaction
            Prajakta_Godbole Level 1
            Okay! this was posted by mistake...I have more explanation to the problem...

            Possible cause to the problem:

            Though the Javascript-Flex connection is set up correctly, the Flex file is not loaded completely before a call is made to pass an input parameter to it. I guess, putting the "Alert pop-up" gives the application sufficient time to load. To have a workaround to this problem, I tried to use setTimeOut in javascript to give the flex application call some delay. Then I tried a while loop to check when the Flex application is ready. But surprisingly, nothing except the "Alert" seems to work. If I take input in text field and pass it to Flex application by using onClick event on Submit button, it works. So problem is with onLoad()...my best guess!

            I tried to validate my html file and checked different character encoding...probably an overkill! Still, no improvement.

            There are several other options to pass input variables to the Flex application.
            1. Flash Satay method
            2.Use SWFObject
            3. Flashvars...(ruled out this one. Because it is included in param tag which some browsers totally ignored. Besides, I think Flashvars is static, what if I want to change my input value?)

            But if I use these, will it fix my problem? Any suggestions?
            • 3. Flex Javascript interaction
              Prajakta_Godbole Level 1
              Hey Tracy! Thanks for replying!

              Here is the ExternalInterface code. I am working on Yahoo API. So you will find some API calls there.

              public var question:Question;

              public function myFunc(s:String):void
              var keyword:String;
              params.results = 50;
              params.type = "resolved";

              searchService.searchQuestionsByTerm(s, params);

              private function setupCallbacks():void

              private function isContainerReady():Boolean
              var result:Boolean = ExternalInterface.call("isReady");
              return result;

              public function startUp():void //initial call made hwn the application starts
              var isAvailable:Boolean = ExternalInterface.available;

              if (ExternalInterface.available)
              var containerReady:Boolean = isContainerReady();
              if (containerReady)
              // If the container is ready, register the SWF's functions.
              trace("External interface is not available for this container.");

              // Define the Event Listeners for the Answer Service
              searchService.addEventListener(AnswersResultEvent.QUESTIONS_SEARCH, questionsResult, false, 0, true);
              searchService.addEventListener(AnswersErrorEvent.ERROR_EVENT, showError, false, 0, true);