Some Problems about the interaction between JavaScript and ActionScript 3.0

Source: Internet
Author: User

The interaction between JavaScript and ActionScript 3.0 is also through the flash. external. ExternalInterface class, but it is different from the Flash. external. ExternalInterface used for interaction with ActionScript 2.0 in flash 8. The biggest difference is that the ExternalInterface. addCallback method has only two parameters in ActionScript 3.0, instead of the instance parameter. All of the questions to be discussed below are about the WordPress 3.0 in Flash 9.

First, the most common problem is that the method does not exist when JavaScript calls the ActionScript method in Flash. This problem occurs when you execute ExternalInterface in Flash. externalInterface. addCallback must be after the HTML is fully loaded, that is, the window. the onload event can be executed only after it is executed. Otherwise, all methods released by the onload event cannot be called in JavaScript.

The solution to this problem has an example in the help of Flash 9's ActionScript 3.0, which contains the solution, that is, first set two logos in js, for example, the jsReady and swfReady variables are used as the flag and are set to false at the beginning. onload, set jsReady to true. In Flash, check whether the jsReady mark in JavaScript is true through ExternalInterface. the call method calls a function that returns this flag in JavaScript). If it is not true, a timer is set. After a period of time, for example, 50 or 100 milliseconds.) repeat this check flag, if it is true, execute ExternalInterface. addCallback to publish the function or method to be provided to JavaScript For calling, and execute After all ExternalInterface. addCallback is complete, use the ExternalInterface. call method to call the function set swfReady flag in JavaScript to set swfReady to true. Then, when JavaScript detects that swfReady is true, it will not encounter this problem if you call the method in ActionScript.

If it is a simple call, it is okay. If there are many such calls, it will be more troublesome. I created two execution Queues: jsTaskQueue and swfTaskQueue. Before jsReady is set to true, if I want to call the operations of the ActionScript, I will put this operation in jsTaskQueue. When js is in window. when jsReady is set in onload, take the tasks in this queue and execute them. If you want to call the operations of ActionScript before jsReady is true and swfReady is true, put this operation in swfTaskQueue. when the call method calls the function setting swfReady flag in JavaScript to set swfReady to true, the tasks in this queue are obtained and executed. When both jsReady and swfReady are true, You can directly run the script if you want to call the ActionScript operation. In this way, after encapsulating these tasks and using these encapsulated operations, you can write the code in order rather than asynchronously. The execution is also executed in order.

In addition to this most common problem, there are also two questions about IE.

If you use JavaScript to dynamically create Flash tags and insert them into html, for example, the innerHTML assignment method or the appendChild method), it is very likely that your operation is in window. in this case, other browsers can normally interact with JavaScript and ActionScript 3.0, and IE won't work. Therefore, to ensure security, the best way is to directly write the html of the flash tag in the html body, or use the JavaScript document. write to write the html body. This method is more suitable for IE, because in this case, you do not need to click to activate Flash.

Another problem is that do not publish a method named invoke in ActionScript. Otherwise, an error occurs when JavaScript calls this method in IE.

The last problem can be found more on the Internet, that is, do not put the flash in the form, otherwise in IE, errors will occur when JavaScript calls ActionScript. Of course, a script is provided on the Internet to solve this problem, but it seems to be for the Flash 8 event 2.0. I have not tried it and I don't know if it is equally effective for event 3.0.

If you call JavaScript through ExternalInterface. call in ActionScript and the passed parameter has a string, if the string contains the \ symbol, the call will fail. This is also a bug in the interaction between ActionScript and JavaScript. The solution is to pass the passed string first. The processing method is very simple. For example, the data to be transmitted is data, perform data once. replace (/\\/, "\\\\") is replaced and passed to JavaScript.

  1. Third day of JavaOne 2009: Microsoft and Sun/Oracle go hand in hand
  2. Develop highly portable j2s Software
  3. Memory settings in Java Virtual Machine (JVM)
  4. Programming Language ranking in June: JavaScript is just getting started
  5. Seven non-abrupt JavaScript rules

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.