views:

80

answers:

3

Hi, for some reason, whenever I dynamically load a swf on a $(document).ready() by writing to a div, I am unable to make javascript calls to the browser. Specifically, all calls to the browser return "null". This does not occur when embedding the swf normally on the page load, but I would like to prevent loading of the swf until a specified point in time.

What the heck is going on here? Is there something special about dynamically embedding a swf that prevents the swf from talking to the browser? The methods ARE called (I've proven such by showing alerts), but all return values to any function, regardless of type returned, shows as null when it gets to flash.

Is this a bug with flex, or am I missing something entirely? This has been a major headache for me.

A: 

I assume you're using ExternalInterface.call (or maybe .addcallback)? Can you post some code (especially from the JS side)?

ajh1138
Yes, ExternalInterface.call.function myFunc(){return "Hey";}In flash I get null.
Stefan Kendall
I'm using swfobject to embed the swf, overwriting document.write to write to a div after the page is loaded.
Stefan Kendall
And by swfobject, I mean the Adobe Deployment Toolkit. This is set to change.
Stefan Kendall
Check out this page: http://livedocs.adobe.com/flex/3/html/help.html?content=19_External_Interface_09.htmlThis part is juicy: "...there is no way to guarantee that a SWF document will register its callbacks before the first JavaScript on the HTML page runs. For that reason, before calling functions in the SWF document from JavaScript, your SWF document should always call the HTML page to notify it that the SWF document is ready to accept connections." The doc has techniques for ensuring that the JavaScript and the SWF can communicate. (Thanks for raising this issue I learned a lot.)
ajh1138
+1  A: 

Switching to swfobject2.2 to embed the swf alleviated the problem magically.

Stefan Kendall
A: 

I ran into this exact problem and got around it in this (slightly icky) way using the Prototype PeriodicalExecuter:

Event.observe(window, 'load', function() {
  //Check to see if the flash function is available once per second.
  new PeriodicalExecuter( function(pe) {
      var flash = $('flashObject');

      if(typeof flash.myFlashFunc == 'function'){
          //At this point, the flash function is available
          pe.stop()
      }
  }, 1);
});

Updating to the latest version is probably the best way to go though. I'll have to try that on my end.

Mark Biek