These two days when doing JavaScript PHPRPC 3.0 clients based on FLASH9 (ActionScript 3.0) encountered some of the problems of JavaScript interacting with ActionScript 3.0, which basically didn't find answers online, Finally through a constant attempt to solve, so summed up here, hoping to give some help to the comrades who have the same problems.
JavaScript interacts with ActionScript 3.0 by Flash.external.ExternalInterface this class, but with the flash.ex used to interact with ActionScript 2.0 in Flash 8 Ternal. The externalinterface is still different. The biggest difference is that the Externalinterface.addcallback method has only 2 parameters in ActionScript 3.0, no more instance this parameter. The following questions are about the ActionScript 3.0 in Flash 9.
The most common problem is to report that the method does not exist when JavaScript invokes the ActionScript method in Flash. This problem is related to the time that the externalinterface.addcallback is executed in Flash, Externalinterface.addcallback must be after the full load of HTML is window.onload The event is executed before it can be executed, or the method it publishes cannot be invoked in JavaScript.
The
Workaround for this problem is an example of the ActionScript 3.0 help in Flash 9, which includes the workaround, which is to first set two flags in JS, such as Jsready and Swfready, as flags, and start with false , set Jsready to True when Window.onload, and in Flash, check to see if this Jsready flag in JavaScript is true (calling Java through the Externalinterface.call method) A function that returns this flag in Script, if not true, set a timer that repeats this check after a period of time (for example, 50 or 100 milliseconds), and once true, executes the Externalinterface.addcallback To publish the function or method that ActionScript wants to provide to JavaScript, and after all the externalinterface.addcallback have been done, call the Externalinterface.call method The function setting for the SWFREADY flag in JavaScript is set Swfready to true. After that, when JavaScript detects that Swfready is true, invoking the method in ActionScript will not encounter the problem of saying so.
If it's a simpler call, it's a hassle if you have a lot of these calls. I did this by setting up two execution queues: Jstaskqueue and Swftaskqueue, and when Jsready is true, if there is an operation to invoke ActionScript, put the operation in Jstaskqueue, when JS is in window. In the onload execution setting jsready, the task in this queue is removed to perform, and if Jsready is true after Swfready is true, if there is an operation to invoke ActionScript, put this action on the Swftaskqueue , the task in this queue is externalinterface.call when ActionScript sets Swfready to True when the function set the Swfready flag in JavaScript is invoked through the call to Java method. When both Jsready and Swfready are true, it is OK to run directly if you have an operation to invoke ActionScript. By encapsulating these tasks in this way, after using these encapsulated operations, the code can be written sequentially (not asynchronously) and executed sequentially.
In addition to this most frequently encountered problem, there are two questions about IE.
If you are dynamically creating a Flash tag through JavaScript and then inserting it into HTML (for example, by InnerHTML or appendchild), it is likely that your operation was performed after Window.onload, in which case , other browsers can interact with JavaScript and ActionScript 3.0 properly, IE not. So, in order to insure, the best way is to write the HTML of the flash tag directly in the body of HTML, or use JavaScript document.write to write HTML body, the latter method is more suitable for IE, because To do so, you may not need to click to activate Flash.
Another problem is that you do not publish a method named Invoke in ActionScript, otherwise in IE, JavaScript calls the method with an error.
The last question, online can be found more, is not to put flash into the form, otherwise in IE, JavaScript invoke ActionScript error. Of course, there is a script to solve this problem on the Internet, but that seems to be for the Flash 8 ActionScript 2.0, I have not tried, do not know whether ActionScript 3.0 is equally valid.
Another problem today is that if JavaScript is invoked through Externalinterface.call in ActionScript, if the passed argument has a string, then if the string contains a symbol, then the failure is invoked. This is also a bug in ActionScript and JavaScript interaction, the solution is to pass the string first processing in the pass, the processing method is very simple, such as the data to be passed is a data.replace (//, "\") After you have replaced it, you can pass it on to JavaScript.