views:

8520

answers:

2

Hi,

I was trying to access swf from javascript, so this example in livedocs is what I'm trying to modify. http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#includeExamplesSummary

However,it is not working correctly for some reason. The problem I'm encountering is that it does not work in Safari and in Firefox, it only works if I put an alert in the function before javascript pass the value to swf. (seems like it needs some time) I also tried to set a timer in as3, but timer doesn't work, only alert in js helps.

All I wanted to do is use js to tell the swf file to play ep1.swf. Here's my js code:

document.observe('dom:loaded', function() {
      $('episode1').observe('click', function() {
       var params = {wmode : "transparent", allowScriptAccess:"always", movie:"header"};
       swfobject.embedSWF("swf/float.swf", "header", "100%", "100%", "9.0.0","expressInstall.swf", "", params, "");
       sendToActionScript("ep1.swf");

      });
     })
function thisMovie(movieName) {

       if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
       } else {
       //alert("aaa")
        return document[movieName];
       }
        }
function sendToActionScript(value) {     
      thisMovie('header').sendToActionScript(value);   
      }

Here's my as3 code:

private function receivedFromJavaScript(value:String):void {

      loader.load(new URLRequest(value));

        }

I've been trying for a really long time, does anyone know how to fix this? Thanks.

+5  A: 

The problem is that the SWF file isn't fully loaded by the time you try to call it. The flash player is probably loaded but it takes a while to load and initialise the swf file.

What you need to do is make a call from the SWF file to a javascript function when it's loaded and put your javascript there rather than in the page loaded handler that you seem to be doing now. That way you know that your flash application is properly initialized by then. The ExternalInterface class you are using has methods to let you call back into the javascript.

John Burton
A: 

Summary of a success:


I am using AC_RunActiveContent.js, as set up by Flash when published.
My swf is called fvar_js, as seen below:

AC_FL_RunContent( ... 'src', 'fvar_js', ...

I STRESS this because I NEVER had to use a function like "thisMovie" in the post above to point to the swf object. I was able to use fvar_js straightaway (well, sort of, as you shall see).


In my as3 code I had the lines:

if (ExternalInterface.available) { ExternalInterface.addCallback("js_to_as_f", js_from_as_f); }

where js_from_as_f was a function that changed the text in a textfield.


In the html I set up the following:

    var timeoutId;
var js_initiate_callback = function() {
 // This is the swf object:
 fvar_js.js_to_as_f();
 clearTimeout ( timeoutId );
}
var reset_fvar_f = function(new_val) {
 fvar_val = new_val;
}
//js_initiate_callback();
timeoutId = setTimeout(js_initiate_callback, 1000);

I tried 1ms and 100ms, but the results were spotty. With 1000ms this worked in IE, FF and Safari on a PC. Have not checked mac.

The key, evidently, is to allow all objects and all object connections time to get set up. I have no idea what the minimum time is.