Interworking Between ActionScript and JavaScript

Source: Internet
Author: User

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

  1. VaR flashplayer;
  2. Function detectflashplayer (){
  3. If (navigator. appname. indexof ("Microsoft ")! =-1 ){
  4. Flashplayer = Window. objectid;
  5. }
  6. Else {
  7. Flashplayer = zookeeper Doc ument. embedname;
  8. }
  9. }
  10. // Body onload
  11. // Call
  12. Flashplayer. showmessage ();
  13. // Call with Parameters
  14. 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

  1. VaR filereference: filereference = new filereference ();
  2. VaR URLRequest: URLRequest = new URLRequest ("http://www.myexamplesite.com/example.txt ");
  3. Filereference. Download (URLRequest );
  4. // The second parameter of the download () method is to specify the file name displayed in the SAVE dialog box.
  5. 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

  1. Try {
  2. Filereference. Download (URLRequest, filename );
  3. }
  4. Catch (illegaloperation: illegaloperationerror ){
  5. // Code to handle an illegal operation Error
  6. }
  7. Catch (Security: securityerror ){
  8. // Code to handle a security error
  9. }
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 );

========================================================== ========================================

Related Article

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.