3 Replies Latest reply on Feb 1, 2008 7:06 PM by greenhippo

    calling Javascript in Flex

    josh_aries Level 1
      Hi

      I have a mxml file that has a button control

      <mx:Button id="toggle" label="Test" click="ToggleButton()" />
      When I click on the button I want to call the javascript function. So in the mxml script I have

      public function ToggleButton(): void
      {
      var f:String = "JSToggleButton";
      var m:String = "this works";
      var check:String;
      if(ExternalInterface.available)
      {
      ExternalInterface.call(f,m);
      check = "true"
      }
      else
      {
      check="false";
      }
      trace(check);
      }
      ]]>


      Now I created a text file by rightclicking on the assets(The folder i created) folder in project and then new-->File.

      I have written my javascript in that file and named it JavaScriptToggle.html

      This file has the following code

      <SCRIPT LANGUAGE="JavaScript">
      function JSToggleButton(m)
      {
      alert(m);
      }
      </SCRIPT>

      It does not seem to show the alert box but the trace shows true for ExternalInterface.available.

      Am I missing anything important?????

      I am completely new to flex. So it might be simple to most of them. Please bear with me.


        • 1. Re: calling Javascript in Flex
          greenhippo Level 1
          Is the javascript file included into the same html document that your swf is embedded into?

          If your using flex builder this might mean changing the index.template.html file so that your javascript function is somewhere wihtin that file
          • 2. Re: calling Javascript in Flex
            josh_aries Level 1
            Hi,

            Thanks for the reply.

            Is the javascript file included into the same html document that your swf is embedded into?

            If I have a project named Test does this mean that I have to incluse my Javascript in the bin folder's Test.html file?

            Is that the file the swf is embedded into?

            • 3. calling Javascript in Flex
              greenhippo Level 1
              quote:

              If I have a project named Test does this mean that I have to include my Javascript in the bin folder's Test.html file?


              Yes, so somewhere in Test.html you will have to have that javascript function. But remember that flex builder will recreate the Test.html file everytime you rebuild and you will lose any changes you make to that file directly. So make your alterations to the html-template/index.template.html file and it will always recreate your Test.html with that code included.

              So just open up html-template/index.template.html and place between the head tags

              &lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
              function JSToggleButton(m)
              {
              alert(m);
              }
              &lt;/script&gt;


              Then everything should hopefully be working