I have never studied the interaction between AS3 and JS, that is, SWF communication in html. Recently, I was forced to invest in it out of project requirements. I will also make a summary here.
I. The fastest way to pass values in HTML
When inserting SWF in HTML, you can add parameters, such:
Files/home.swf? Id = "21 ″
In AS3, you can receive
Var id: String = stage. loaderInfo. parameters ["id"]; this method of passing values is actually used in AS2, in AS3, the parameters attribute is used to replace AS1.0 and 2.0 to provide SWF file parameters as the main timeline technology, so we can use the AS3 value method.
This method can only be used for one-time value transfer by one side. Sometimes it is used, but it cannot communicate with JS. In addition, it can only receive strings.
Ii. Use the external communication interface ExternalInterface
This is also the most reliable communication method that has evolved from the AS2 era.
Usage:
1. JS calls the AS3 Function
Before JS calls AS3, it is necessary to bind the call function in as3. that is to say, only the method JS that AS3 promises can be called.
Use: ExternalInterface. addCallback (functionName: String, closure: Function): void has two parameters:
FunctionName: String-container name that can be used to call a function.
Closure: Function-the closure Function to be called. This may be an independent function, or it may be a reference object instance method.
ExternalInterface. addCallback ("getASVars", getASFun); private function getASFun (value: String): void {// get the value from JS: value}
Okay, you can call this method in JS. Before calling this method, you must first obtain the swf id inserted into HTML. We can set the ID in HTML no matter how it is inserted.
For example, first obtain the object through id "mov" and then call the method bound above:
The Code is as follows: function thisMovie (movieName) {if (navigator. appName. indexOf ("Microsoft ")! =-1) {return window [movieName];} else {return document [movieName] ;}} thisMovie ("mov "). getASVars (value); 2. AS3 calls JS Functions
You can call JS directly in AS3.
Use: ExternalInterface. call (functionName: String ,... Arguments ):*
This method has two parameters:
FunctionName: String-name of the function to be called in the container.
... Arguments-parameter of the function passed to the container. You can specify zero or multiple parameters, which are separated by commas. These parameters can be of any ActionScript data type. When a JavaScript function is called, The ActionScript type is automatically encapsulated into the JavaScript type. When another ActiveX container is called, the parameters are encoded in the request message.
For example:
The Code is as follows: ExternalInterface. call ("setToJS", "paramTest"); // write this setToJS function in JS: senToJS (value) {alert (value) // output: paramTest}
It looks great, but it is not completely reliable. This interface will still be picky, only supported in the following browsers: in the browser operating system, Internet Explorer 5.0 and later Windows Netscape 8.0 and later Windows macdeskshmozilla 1.7.5 and later Windows macdeskshfirefox 1.0 and later Windows macdeskshsafari 1.3 and later Windows macdeskshsafari and later Macintosh have a Security Sandbox problem, if a Security Sandbox alarm occurs, you can use either of the following methods: 1. in the object tag of the SWF file that contains the HTML page, set the following parameters: 2. in the SWF file, add the following ActionScript:
The Code is as follows: Flash. system. Security. allowDomain (sourceDomain)