1. Call Javascript in ActionScript
The call () method requires at least one parameter to specify the JavaScript function name.
Externalinterface. Call ("changetitle ");
If the JavaScript function requires parameters, add the parameters in the call () method. For example, the changetitle () function accepts a parameter:
Externalinterface. Call ("changetitle", "ActionScript 3.0 cookbook ");
========================================================== ====================================
2. Javascript calls ActionScript
Use externalinterface. addcallback () to register the ActionScript function and call it on the Javascript side.
The addcallback () method receives two parameters: the first parameter is a string-type function name, And the browser uses this name to know the function to be called, the second parameter is the actual ActionScript function to be executed when the browser calls the defined function name. The following example registers the displaymessage function, and the alias in Javascript is showmessage:
Externalinterface. addcallback ("showmessage", displaymessage );
There are two types of Flash players in the browser: ActiveX (Tags) and plug-in (Tag) version
Example:
HTML code
- VaR flashplayer;
- Function detectflashplayer (){
- If (navigator. appname. indexof ("Microsoft ")! =-1 ){
- Flashplayer = Window. objectid;
- }
- Else {
- Flashplayer = zookeeper Doc ument. embedname;
- }
- }
- // Body onload
- // Call
- Flashplayer. showmessage ();
- // Call with Parameters
- Flashplayer. showmessage ("Example Message ");
// Body onload // call flashplayer. showmessage (); // call the parameter flashplayer. showmessage ("Example Message ");
========================================================== ======================================
3. PASS Parameters to SwF through Param of tags in HTML
The HTML part must be inAddTag,
You can use the root. loaderinfo. Parameters attribute of any visualization component to obtain the parameters passed by flashvars. The root. loaderinfo. Parameters attribute is an associated array.
========================================================== ======================================
4. Download an object
As code
- VaR filereference: filereference = new filereference ();
- VaR URLRequest: URLRequest = new URLRequest ("http://www.myexamplesite.com/example.txt ");
- Filereference. Download (URLRequest );
- // The second parameter of the download () method is to specify the file name displayed in the SAVE dialog box.
- Filereference. Download (URLRequest, "11.txt ");
VaR filereference: filereference = new filereference (); var URLRequest: URLRequest = new URLRequest ("http://www.myexamplesite.com/example.txt"); filereference. download (URLRequest); // The second parameter of the download () method is to specify the file name filereference displayed in the SAVE dialog box. download (URLRequest, "11.txt ");
The download () method is best executed in try... catch statements, because this method may throw an exception, mainly two types of exceptions:
Illegaloperationerror and securityerror. When the download () method is called when the Save dialog box is opened, illegaloperationerror is thrown. The securityerror is caused by the failure of SwF download.
Example:
As code
- Try {
- Filereference. Download (URLRequest, filename );
- }
- Catch (illegaloperation: illegaloperationerror ){
- // Code to handle an illegal operation Error
- }
- Catch (Security: securityerror ){
- // Code to handle a security error
- }
try {fileReference.download(urlRequest, fileName);}catch (illegalOperation:IllegalOperationError) {// code to handle an illegal operation error}catch (security:SecurityError) {// code to handle a security error}
Filereference. addeventlistener (ioerrorevent. io_error, onioerror );
========================================================== ======================================
5. File Download progress
Filereference. addeventlistener (progressevent. Progress, onfileprogress );
Private function onfileprogress (Event: progressevent): void {
Fileprogressfield. Text = event. bytesloaded + "of" + event. bytestotal + "bytes ";
}
When the download is complete, the filereference object sends a complete event. The type is event:
Filereference. addeventlistener (event. Complete, onfilecomplete );
========================================================== ======================================
6. Browse the file selected in the dialog box
I. Filtering
VaR filefilter1: filefilter = new filefilter ("Images", "*. PNG; *. gif; *. jpg ");
VaR filefilter2: filefilter = new filefilter ("documents", "*. txt; *. Doc; *. pdf; *. rtf ");
VaR filefilter3: filefilter = new filefilter ("Archives", "*. Zip; *. tar; *. hqx ");
VaR filefilter4: filefilter = new filefilter ("all ","*.*");
_ Filereference. Browse ([filefilter1, filefilter2, filefilter3, filefilter4]);
2. Select
After you select a file and click the OPEN button, the filereference object will issue a select event.
Filereference. addeventlistener (event. Select, onselectfile );
// Selectedfiletextfield. Text = filereference. Name;
Or
Filereference. addeventlistener (event. Cancel, oncancelbrowse );
========================================================== ========================================