Processing javascript events in flash/flex programs

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.