在flash/flex程式中處理javascript的事件

來源:互聯網
上載者:User

To get started you will need to download the library JSInterface from 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

            var url: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.document - 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 {

      import aw.external.JSInterface;

     

      import flash.display.Sprite;

     

      [SWF(width="20", height="20")]

      public class Test extends Sprite{

            public function Test():void{

                  super();

                  JSInterface.initialize(this);

                  JSInterface.document.body.onclick = this.onclickHandler;

            }

            protected function onclickHandler():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.document.getElementById('clickButton').onclick = this.onclickHandler;

Happy clicking!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.