6 Replies Latest reply: Mar 5, 2010 3:46 PM by IlluminatiBG RSS

    ExternalInterface always return null from IE

    IlluminatiBG Community Member

      I copied following code from the ExternalInterface example to my flash:

       

                  if (checkJavaScriptReady()) {
                      console.appendText("Javascript is ready.\n");
                  } else {
                      console.appendText("Waiting for javascript ...");
                      var readyTimer:Timer=new Timer(500,0);
                      readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
                      readyTimer.start();
                  }
      function checkJavaScriptReady():Boolean {
          var isReady:Boolean=ExternalInterface.call("jsReady");
          return isReady;
      }
      function timerHandler(event:TimerEvent):void {
          var isReady:Boolean=checkJavaScriptReady();
          if (isReady) {
              console.appendText("\nJavaScript is ready.\n");
              Timer(event.target).stop();
          } else {
              console.appendText(".");
          }
      }
      

      in Firefox everthing works OK and half a second after a page loads I get "Javascript is ready." message. In Internet Explorer I get only dots appearing. Here is the JS code:

       <script type="text/javascript" language="JavaScript">
           var isReady = false;
           function jsReady() {
               return isReady;
           }
           function pageInit() {
               isReady = true;
              }
      </script>
      </head>
      <body onload="pageInit();">
      

      I try to debug the javascript in Internet Explorer and I trace it to function __flash__toXML inside DOM object for the movie, it checks the return type value and return a XML tag <true/> of <false/> depending on the value.

      function __flash__toXML(value) {
         var type = typeof(value);
          if (type == "string") {
              return "<string>" + __flash__escapeXML(value) + "</string>";
          } else if (type == "undefined") {
              return "<undefined/>";
          } else if (type == "number") {
              return "<number>" + value + "</number>";
          } else if (value == null) {
              return "<null/>";
          } else if (type == "boolean") {
              return value ? "<true/>" : "<false/>";
          } else if (value instanceof Date) {
              return "<date>" + value.getTime() + "</date>";
         } else if (value instanceof Array) {
             return __flash__arrayToXML(value);
         } else if (type == "object") {
             return __flash__objectToXML(value);
         } else {
              return "<null/>"; //???
          }
      }
      

      When I debug an ExternalInterfaceExample just after the return statement it jumps to the code it executed before, but when I debug my application it goes to:

      try { __flash__toXML(jsReady()) ; } catch (e) { "<undefined/>"; }
      

      I also get a runtime error on unloading because it releses the ExternalInterface.callback that does not exist. Calling a callback defined with ExternalInterface.addCallback and the JS says that function is not defined (but it seems movie DOM javascript tries to free that function, I suppose it also trying to define it).

      Any suggestions? What I am doing wrong? The ExternalInterfaceExample works ok, why my flash it doesn't?

        • 1. Re: ExternalInterface always return null from IE
          kglad MVP

          try:

           

          IlluminatiBG wrote:

           

          I copied following code from the ExternalInterface example to my flash:

           

                      if (checkJavaScriptReady()) {
                          console.appendText("Javascript is ready.\n");
                      } else {
                          console.appendText("Waiting for javascript ...");
                         var readyTimer:Timer=new Timer(500,1);
                          readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
                          readyTimer.start();
                      }
          function checkJavaScriptReady():Boolean {
              var isReady:Boolean=ExternalInterface.call("jsReady");
              return isReady;
          }
          function timerHandler(event:TimerEvent):void {
              var isReady:Boolean=checkJavaScriptReady();
              if (isReady) {
                  console.appendText("\nJavaScript is ready.\n");
                  Timer(event.target).stop();
              } else {
                  console.appendText(".");
              }
          }

          in Firefox everthing works OK and half a second after a page loads I get "Javascript is ready." message. In Internet Explorer I get only dots appearing. Here is the JS code:

           <script type="text/javascript" language="JavaScript">
               var isReady = false;
               function jsReady() {
                   return isReady;
               }
               function pageInit() {
                   isReady = true;
                  }
          </script>
          </head>
          <body onload="pageInit();">
          

          I try to debug the javascript in Internet Explorer and I trace it to function __flash__toXML inside DOM object for the movie, it checks the return type value and return a XML tag <true/> of <false/> depending on the value.

          function __flash__toXML(value) {
             var type = typeof(value);
              if (type == "string") {
                  return "<string>" + __flash__escapeXML(value) + "</string>";
              } else if (type == "undefined") {
                  return "<undefined/>";
              } else if (type == "number") {
                  return "<number>" + value + "</number>";
              } else if (value == null) {
                  return "<null/>";
              } else if (type == "boolean") {
                  return value ? "<true/>" : "<false/>";
              } else if (value instanceof Date) {
                  return "<date>" + value.getTime() + "</date>";
             } else if (value instanceof Array) {
                 return __flash__arrayToXML(value);
             } else if (type == "object") {
                 return __flash__objectToXML(value);
             } else {
                  return "<null/>"; //???
              }
          }
          

          When I debug an ExternalInterfaceExample just after the return statement it jumps to the code it executed before, but when I debug my application it goes to:

          try { __flash__toXML(jsReady()) ; } catch (e) { "<undefined/>"; }
          

          I also get a runtime error on unloading because it releses the ExternalInterface.callback that does not exist. Calling a callback defined with ExternalInterface.addCallback and the JS says that function is not defined (but it seems movie DOM javascript tries to free that function, I suppose it also trying to define it).

          Any suggestions? What I am doing wrong? The ExternalInterfaceExample works ok, why my flash it doesn't?

          • 2. Re: ExternalInterface always return null from IE
            IlluminatiBG Community Member

            I tried it, but this check if JS is available only once after 500ms and then finish the script in Action panel. The idea is to check again and again until th page is loaded. This is to make sure that both application (flash and browser script) are ready to communicate (e.g. flash finished loading and page finished loading). However ExternalInterface.available is true, IE supports such calls, allowScriptAccess is set to sameDomain (and .swf is placed in the same directory as .swf file), but ExternalInterface.call returns an object that converted to boolean is always false. ExternalInterface.addCallback don't work at all, it does not throw any Error, but new function is not added as a property of the movie. That only happens on IE, and only to my movie

            • 3. Re: ExternalInterface always return null from IE
              IlluminatiBG Community Member

              I found the source of the problem. It was missing attribute to the tag <object> which seems in IE classid is required.

              • 4. Re: ExternalInterface always return null from IE
                roidelapatate

                Which attribute was this?

                Thanks.

                • 5. Re: ExternalInterface always return null from IE
                  5barns Community Member

                  Make sure there's an "id" attribute on your <object> tag. The IE way of handling ExternalInterface.call(...) is to call a method on the <object>. IE issues the code

                   

                      document.getElementById("...").SetReturnValue(...)

                   

                  Without an id attribute on your object tag, this reduces to

                   

                   

                      document.getElementById("").SetReturnValue(...)

                   

                  which tries to call SetReturnValue on a null object.

                  • 6. Re: ExternalInterface always return null from IE
                    IlluminatiBG Community Member

                    Yes, IE use ID attrubute, but without classid the flash plugin doesn't initialize properly into page. There must be something that tells the IE that the object is a flash.

                     

                    However there is a solutions:

                    1. Use <object> and <embed> tag and made the page works in quirk mode, since <embed> tag is extension of some browsers (not recommend)

                    2. Use only <object> tag and use DATA="path/to/file.swf" to specify the movie, also include TYPE="application/x-shockwave-flash" which will tell the browser (including IE 6.0 and later, Firefox (tested only on 2.x, 3.0.x, 3.5.x and 3.6.x), Opera (tested on 10.0)). You can also include <param> tags to redefine information, but I didn't tested if it makes any difference. This method will not break standard compiliance mode, but the cost will be, that the movie will not show, until it loaded completely. So it must be a loader, or it must be small (which works for me).

                     

                    In any other way there was a problem with showing the movie or ExternalInterface in most used browsers.