To get started you will need to download the library JSInterface from the http://code.google.com/p/jsinterface/
Then, create a project and add (Properties-> ActionScript Build Path-> Library path-> Add SWC ...) the library to the project. it is necessary to initialize JSInterface, prior to use. to do so you need call the method JSInterface. initialize (). this method as the first argument accepts any object which can tell URL from which loaded this flash/flex program.
The value of this argument can be:
<! -- [If! SupportLists] --> · <! -- [Endif] --> Any DisplayObject added to the stage
JSInterface. initialize (This. Stage );
<! -- [If! SupportLists] --> · <! -- [Endif] --> LoaderInfo object of the current program
JSInterface. initialize (This. LoaderInfo );
<! -- [If! SupportLists] --> · <! -- [Endif] --> String URL
VarUrl: String =Http://someserver.com/main.swf";
JSInterface. initialize (url );
This argument is required if HTML page do not specify ID for this SWF.
The last thing you need to do is create a function and assign it a specific JavaScript object as a parameter. to access the JavaScript object you need to use class JSInterface and top level DOM object from the root structure of HTML page.
<! -- [If! SupportLists] --> · <! -- [Endif] --> JSInterface. window-JavaScript window object
<! -- [If! SupportLists] --> · <! -- [Endif] --> JSInterface.doc ument-JavaScript document object
<! -- [If! SupportLists] --> · <! -- [Endif] --> JSInterface. navigator-JavaScript navigator object
<! -- [If! SupportLists] --> · <! -- [Endif] --> JSInterface. main-JavaScript HTMLElement object which contain currect instance of flash player
For example, I assign a function to handle event click HTML document:
Package{
ImportAw. external. JSInterface;
ImportFlash. display. Sprite;
[SWF (width ="20", Height ="20")]
Public ClassTestExtendsSprite {
Public FunctionTest ():Void{
Super();
JSInterface. initialize (This);
JSInterface.doc ument. body. onclick =This. OnclickHandler;
}
Protected FunctionOnclickHandler ():Void{
Trace('Test');
}
}
}
This example can not work locally, if so-start debug session over HTTP.
You can change this example to handle click event from HTML button.
Add button to default HTML template:
<Input type = "button" id = "clickButton" value = "Click me! "/>
Change JSInterface object using buttons ID:
JSInterface.doc ument. getElementById ('Clickclickclick'). Onclick =This. OnclickHandler;
Happy clicking!