javascript – How to detect that the swf has been loaded

Question:

I would like to know if it is possible to detect when the swf is loaded:

Here's the code:

<object id="teste"  name="teste" type="application/x-shockwave-flash" data="120x600.swf" width="550" height="400" onload="alert('carregado')">
   <param name="movie" value="120x600.swf"/>
   <param name="quality" value="high"/>
   <param name="allowscriptaccess" value="always"/>
   <a href="http://www.adobe.com/go/getflash">
   <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player"/>
   </a>
</object>

* I tried using the onload parameter but it doesn't seem to work on the object tag

* Ah, I can't use swfObject , project limitations.

Answer:

An alternative is to use the ExternalInterface for communication between JavaScript and ActionScript. With it you can pass commands from JavaScript to ActionScript and vice versa.

First you can hear your SWF load using the code below in the first frame of the application:

this.loaderInfo.addEventListener(Event.OPEN, iniciouCarregamento);
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, carregando);
this.loaderInfo.addEventListener(Event.COMPLETE, carregouSWF);

function iniciouCarregamento(e:Event):void {
    trace("Iniciou carregamento");
}

function carregando(e:ProgressEvent):void {
    trace("Carregando..."+Number((e.bytesLoaded/e.bytesTotal)*100));
}

function carregouSWF(e:Event):void {
    trace("Carregou o SWF!");
    ExternalInterface.call("console.log", "Carregou o SWF!"); //Chama uma função do javascript, no caso, o "console.log", e passa como parâmetro a string "Carregou o SWF!"
}

In javascript you can use a custom function and call it by the same method like this:

Javascript:

function escutarSWF(param) {
     console.log(param);
}

ActionScript:

ExternalInterface.call("escutarSWF", "Olá! Eu sou o ActionScript!");

Because both languages ​​are ECMAScript inheritance, the communication between them is efficient and functional, I believe you can use them together.

I don't know if there is any other alternative, but for pure JavaScript I think this one is the most reliable.

Scroll to Top