Search code examples
javascriptflashdom-eventscommunication

JavaScript and SWF communication


I have a sizeable interactive swf file and the file is embedded to my HTML using SWFObject. I can communicate with the swf by JavaScript and it works perfectly. But it has no preloader and because the file is big I want to show a loading image or swf and when the file is loaded completely show my play button using JavaScript.

How can I understand, if the file is loaded completely?

I tried bunch of solutions but none of them was successful. First, I tried to create a preloader in Flash and load my external swf then send a message to JavaScript on complete event using externalinterface, it worked, but I couldn't communicate to the main swf Action Scripts by JS anymore.

I found some JavaScript libraries that are supposed to fire an event, when the file is loaded but it happens when the loading is successful (the swf file is there and starts loading).


Solution

  • I would also strongly recommend you incorporate your play/pause, etc., functionality directly into your Flash program - there is really no need to use JavaScript for this!

    But if you have to use JS, you could show the load progress by creating an internal preloader, or loading an external swf into the same application domain (whereas if you don't use JS, you don't need to worry about application domains), or using SWFBridge to establish two-way communications between two separate SWFs.

    The internal preloader is a pretty neat solution, if you want all of your data embedded into just one file - at the expense of having to create additional frames, and having to think about where exactly to put your class instances in your FLA (you can't use "Embed into frame 1").

    Loading into the same application domain is more elegant, especially if you use your Flash IDE mostly for coding (and not design), or if you have external data anyway. Plus, it's a good way to create modular applications.

    SWFBridge is really good if you have ActionScript 2 SWFs, or legacy files without directly available source code - but if all you need is a simple preloader and some JavaScript, I would probably not use it in this case.