How to use javascript to access SWF function _ javascript skills

Source: Internet
Author: User
This article mainly introduces the usage of functions used by JS to access SWF. The example analyzes the methods and error-prone processing skills of javascript to access swf files, for more information about how to use JavaScript to access SWF, see the examples in this article. Share it with you for your reference. The specific analysis is as follows:

There have been many examples of mutual calls between Flash and JS. This is not a problem. Of course, call errors are not Flash and JS errors. We will solve IE completely today, incompatibility between FF and Chrome!

We know that in Flash, If you access external JS functions, you only need

The Code is as follows:

Flash. external. ExternalInterface. call ("function name in JS ");

This sentence is enough.

If you want JS to access functions in Flash, you need to register a callback function with addCallBack for JS to call (AS2 example)

Import flash. external. *; var methodName: String = "SetImgPath"; // name of the Function to be called by JS: var instance: Object = null; var method: Function = extractstr; // The actual function name in Flash. The parameter is ignored. However, when calling the function, remember to add var wasSuccessful: Boolean = ExternalInterface to the parameter. addCallback (methodName, instance, method); trace (wasSuccessful + "-callback ")

We can trace it. If it is true, the registration is successful.

OK. Try it on the web below:

OK. We add a button on the Web to call this flashcall:

Test:

IE6, 7, 8, and 9 are all normal.

FF: Failed

Chrome: Failed

Cause of failure: SetImgPath is not a function (is not a function/not defined)

This is very strange. We can use an alert to output

The Code is as follows:

ThisMovie ("123"): alert (thisMovie ("123 "));

The output is not Null, indicating that our Flash has been found, but why does FF and Chrome always fail to find the function?

When I visited various forums, I agree that addCallback must be successfully registered after flash is loaded. Well, we can add a settimeout Function to determine whether the loading is successful, let's change the above flashcall:

Function flashcall (str) {try {thisMovie ("123 "). setImgPath (str); // note that 123 is the above ID, and SetImgPath is the callback function registered in Flash} cache (e) {settimeout ("flashcall (str )", 100 );}}

Well, we use settimeout to call it every 100 milliseconds. If an exception occurs, it will continue until the call is successful!

Unfortunately, it never succeeds and the program enters an endless loop!

After reading various materials, I finally found the cause:

In FF, the browser only recognizes the embed tag. Therefore, if you use getElementById to obtain the flash, you need to mark the ID of the embed, while IE knows the object tag, so you need to mark your ID on the object

Do you understand? In FF and Chrome, Flash must use embed to identify its ID. The ID cannot be identified by using Object tags. The Html I use is automatically generated by Flash, they are all Object tags. After debugging for one afternoon, I realized that the problem was solved here.

If you know the reason, you can handle it. Modify the tag:

      

Change the label embedded in Flash to the one above. Everything is OK!

I hope this article will help you design javascript programs.

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.