Listen for Flash events in JavaScript (GO)

Source: Internet
Author: User

There are two ways to listen for Flash events in javascript:

1, in a specific environment (for example, specially crafted flash), we agreed to a global function, and then in the Flash event with Externalinterface.call call this global function.
2, in a non-specific environment (such as writing a common Flash plug-in), can not restrict the user's function name, so there is no way to contract the global function, can be similar to JS-like callback function to implement event monitoring?

In fact, JS and flash communication, under normal circumstances can be carried out some relatively simple communication, such as the transmission of basic data types, transfer simple objects, call functions, etc., but function (that is, functions, the callback function is definitely an instance of the function Class) is not in the basic data type, It cannot be passed.

I don't know if there's any other way to pass it, but at least in my own experiment I haven't been able to make it.

So, if it is the second case, it is generally easier to implement an API in Flash, pass a JS global function name through this API, and then invoke the function in flash events, such as:

Enter the following as code in Flash:

import Flash.external.ExternalInterface;varEvents:object = {};//Register JS API that can be calledExternalinterface.addcallback ("AddEventListener", AddEventListener); function AddEventListener (key:string=NULL, value:string =NULL): boolean{if(Key = =NULL|| Value = =NULL)return false; Switch(key) { Case " Complete": Events.complete=value; return true; //Add other events in a similar way    }    return false;}

Then in an event in Flash, call the JS function:

import flash.events.event;import flash.external.ExternalInterface; function Complete_handler (Event        : Event):void{  ifnull) Externalinterface.call (events.complete);        / *  If you need to pass parameters, use the following sentence: Externalinterface.call (events.complete, parameter 1, parameter 2 ...); */}

Now, JS can listen to the time through the following code:

// SWF is your flash object, get it Yourself swf.addeventlistener ("Complete", Complete_ handler); function Complete_handler () {alert (" event monitoring succeeded! ");}

However, the above monitoring method has a disadvantage, that is, if the parameters of the event is not fixed, parameter passing can be cumbersome. Or in some cases I would like to use a callback function to listen to the Flash event, then you can do an event proxy in JS:

varFlash ={types: {},Event: function (type) {varListeners = This. Types[type]; if(listeners) { for(vari =0; i < listeners.length; ++i) {listeners[i] (); }}}, Addeventlistener:function (type, fn) {if(! This. Types[type]) {             This. types[type] = []; }         This. Types[type].push (FN); }}; Flash.addeventlistener ("Load", load); Flash.addeventlistener (" Complete", function () {alert ("Loading complete! ");}); function load () {alert ("Loading! ");}

Then call the following code in the Flash event:

import flash.events.event;import flash.external.ExternalInterface; function Load_handler (Event: Event):void{Externalinterface.call ("flash.event","Load");} function Complete_handler (Event: Event):void{Externalinterface.call ("flash.event"," Complete");}

If anyone has new methods or ideas, please feel free to advise!

Listen for Flash events in JavaScript (GO)

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.