7 Replies Latest reply on Nov 5, 2007 1:56 PM by Greg Dove

    exernalInterface (argh!)

    jmoggio
      I managed to get my flash to call an external js function successfully, but can't get a js function to call back in. Hoping I'm just missing some dumb little thing. I've looked at a bunch of samples and can't figure it out...

      Flash movie name, id in the HTML is listenInLibraryPlayer
      In the Flash movie, I have...

      import flash.external.*;
      function newMedia(idNumber){
      testText.htmlText = idNumber;
      }
      ExternalInterface.addCallback("sendID", null, newMedia);


      In the HTML head I have code I got from adobe.com...

      <SCRIPT LANGUAGE=JavaScript>
      function callExternalInterface() {
      getMovieName("listenInLibraryPlayer").sendID(str);
      }

      function getMovieName(movieName) {
      if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName]
      }
      else {
      return document[movieName]
      }
      </script>


      In the HTML body I have...

      <form name="inputValue">
      ID Number <input type="text" name="x" size="10" ><BR/><BR/>
      Enter 1 or 2.
      </form>
      <form>
      <input type="button" onClick="callExternalInterface()" value="Call Flash Function" />
      </form>
        • 1. Re: exernalInterface (argh!)
          kglad Adobe Community Professional & MVP
          is listenInLibraryPlayer.swf the name of your embedded swf? and where's str coming from?
          • 2. Re: exernalInterface (argh!)
            jmoggio Level 1
            That is the name of the .swf.

            I have no idea about the HTML and the javascript - I just took some code from a sample.

            I have a new version which uses links instead of a form (and also fails, but is closer to what I eventually want)...

            <a name="text_link" id="text_link" onclick='callExternalInterface("1");' >Click Here for 1</a>

            <a name="text_link" id="text_link" onclick='callExternalInterface("2");' >Click Here for 2</a>



            • 3. exernalInterface (argh!)
              Greg Dove Level 4
              Ok, kglad already asked where str is coming from. You need that as an argument in your javascript callExternalInterface function.

              Then if that's not working I would check that your generated id and name tags are as follows:

              your object tag id atttribute is "listenInLibraryPlayer" and the embed tag name attritbute is also "listenInLibraryPlayer"

              If you're using a script (like swfobject or adobe's activecontent.js etc) then you need to make sure you have it set up to generate the correct embedding output to give the correct id and name attributes
              • 4. Re: exernalInterface (argh!)
                jmoggio Level 1
                Thanks to all for replying, but I'm still not getting this thing to work (argh!).

                As far as I can tell, the name and id are correct everywhere, and I have no idea what's wrong. If anyone is feeling really generous, the files are all located in a zip here - maybe someone can figure out what I'm missing...

                www.mogeworks.com/testing
                • 5. Re: exernalInterface (argh!)
                  Greg Dove Level 4
                  There's three problems I see.
                  1. You need to add an argument (str) to your javascript callExternalInterface method.
                  2. I don't think "1" or "2" work as arguments. I used an argument the same as your internal buttons to get it to work, e.g.
                  callExternalInterface('Storage 2'); or callExternalInterface('Storage 1');
                  2. You have multiple object/embed tags with the same id/name pairs, which prevents the reference for the external call working properly. I got it working in firefox by stripping it down to just one embed tag. The noscript wrapped embedding code seems to interfere with this as well.
                  The way I would approach it is this: use swfobject to do the embedding and a ) remove the noscript tags if the player version is >= 8. Then use the write method of the swfobject instance to write the embedding code to a div target. swfobject only ever inserts the appropriate embedding code for the browser its being used in so you don't get duplicate ids.

                  If I get time tomorrow and you haven't managed to get it working I'll provide a working example... but those three things make it work.
                  • 6. Re: exernalInterface (argh!)
                    jmoggio Level 1
                    Thanks very much for taking the time. I think the major problem was not having the str variable in the parentheses for the javascript function. All the extra code for the version detection and active content doesn't cause problems in Safari, but it does kill it in Firefox. Haven't tested IE yet.

                    Thanks again.
                    • 7. Re: exernalInterface (argh!)
                      Greg Dove Level 4
                      You're welcome. There may be some browser differences with the handling of id tags etc. I didn't try it in IE or safari. Its not the version detection code or embedding code that kills it... it seems to be the resulting embedded code. If you have 2 or more tags with the same id attribute then ExternalInterface doesn't (shouldn't?) work correctly. But if it still works in safari then I guess that doesn't hold true for all browsers.