3 Replies Latest reply on Jun 17, 2009 2:13 AM by PurpleArts

    Calling Functions from JavaScript

    Sairus101

      Greetings,

      First of, my apologies if this has been answered before.

       

      I'm fairly new to ActionScript and even newer to JavaScript, so I'm having some trouble reading a JavaScript function from the HTML file I've embedded my SWF in.

      I'm currently using Flash 8.

      For simplicity's sake, I'm just trying to change the text in a MovieClip from "Default" to the return value of a function in Javascript.

      However, when I use the following code, the text is just staying as Undefined.

      Any help you can offer would be greatly appreciated!

       

       

      Flash Code:

      import flash.external.*;

      var test:String= "Default";


      test = ExternalInterface.call(callJavaScript()).toString();
      testbutton.buttonText.text = test;

       

       

      Javascript Code:

      //Placed within the Head tags of my HTML file

       

      <script language="JavaScript">
          function callJavascript() {

               return "test"
          }
      </script>

       

      //later on I'm setting AllowScriptAccess to "Always"

        • 1. Re: Calling Functions from JavaScript
          PurpleArts Level 1

          are you trying to call your javascript and pass values to it? or is it the other way around?

           

          either way, your usage of externalinterface is not accurate....

           

          if your trying to pass a value to the js, you use, externalinterface.call but if your trying to get a value from js, you should use, externalinterface.addcallback.

          1 person found this helpful
          • 2. Re: Calling Functions from JavaScript
            Sairus101 Level 1

            Cheers for the response,

             

            I'm trying to get a value back from the JS.

            I've just modified my code by replacing 'call' with 'addCallback', but now instead of 'undefined', my text is changing to 'false'.

             

             

             

            Flash Code:

            import flash.external.*;

            var test:String= "Default";


            test = ExternalInterface.addCallback(callJavaScript()).toString();
            testbutton.buttonText.text = test;

             

             

            Javascript Code:

            //Placed within the Head tags of my HTML file

             

            <script language="JavaScript">
                function callJavascript() {

                     return "test"
                }
            </script>

             

            //later on I'm setting AllowScriptAccess to "Always"

            • 3. Re: Calling Functions from JavaScript
              PurpleArts Level 1

              here is a sample where a textbox from html sends a value to actionscript

               

              var objectInstance:Object;

               

              ExternalInterface.addCallback("sendTextToFlash",objectInstance,getTextFromJavaScript);
              //Function to verify that the url has a value
              function getTextFromJavaScript(str:Object):Void

                  received_ti.text = "From JavaScript: " + str;
                  label_received.text= str;
              }//end function

               

               

              html:

               

              function getFlashMovie(FlashMovie) {  
              var isIE = navigator.appName.indexOf("Microsoft") != -1;  
              return (isIE) ? window[FlashMovie] : document[FlashMovie];  }

               


              function formSend(text)
              {   var text = document.htmlForm.sendField.value;  
              getFlashMovie1("Indexl").sendTextToFlash(text);     }

               

              <form name="htmlForm" method="POST" action="javascript:formSend();"> 
              Received from ActionScript:<input type="text" name="receivedField" disabled="true"/>
              </form>