JavaScript events in Flash
Tuesday 22 March 2005 – Filed under: Flash – No Comments
There are a number of full-blown solutions on the market that provide you with two-way JavaScript <> Flash communication. I needed something similar, but much simpler: translate JavaScript functions to EventDispatcher events in Flash.
I started of with some Googlin’, but I couldn’t really find what I needed. And although this ActionScript 1 implementation by Bokel got close, it is based on the fact that you must specify the exact location of the Flash function to trigger, _level0.anObject.itsMethod, for example.
I like an events based solution much better, as that gives you more flexibility and keeps your code clean, so I wrote an ExternalFunction class this afternoon that allows you to subscribe Flash objects to any JavaScript event you wish to pass to your Flash application by using a regular .addEventListener() call.
In ActionScript:
import ExternalFunction;
import ExternalFunctionEvent;
var mExtFunc:ExternalFunction = ExternalFunction.getInstance();
mExtFunc.addEventListener("myCutomEvent", this);
var myCutomEvent:Function = function ( e:ExternalFunctionEvent ) {
// code goes here...
}
In HTML1:
<a href="javascript:flashEvent('upload', 'onImageUploaded','image_01.jpg');">view image 1</a>
There’s an online example available. Download the source code here. I also wrote some documentation for this class, which can be found here.
As I have just completed the code, there may be some unexpected rough edges, so be warned. If you encounter any weird stuff, let me know.
1 Please note that you need to carefully name your main Flash application using the ‘id’ parameter in the object tag and also set the same name in the embed tag using the ‘name’ parameter. This id is used as the name of the LocalConnection to setup and is passed to both the main Flash application and the gateway.way through FlashVars.
« previously: Playing WoW next: FlashEurope a scam: it’s “official” »
Leave a Reply
Recent Articles
- Running Google Chrome OS under Parallels
- AS3 Tweetr library suffers from the Twitpocalypse bug
- Digital TV on Mac Mini, part 2
- Digital TV on Mac Mini, part 1
- New bike
- FLV Player 2.0.25 released
- No custom headers for Flash file upload. Why?
- Building AIR with FDT
Advertisement
Categories
- Art & Design (4)
- Flash (38)
- FLV Player (9)
- Software (17)
- User Experience Design (3)
- Various (10)
- Work (6)