Directory • Parameter pass JavaScript call Flash Method • Method Performance Tuning parameters passed back to directory
When JavaScript calls a Flash method, Flashplayer converts the arguments to XML format, which is actually thrown into the global when Flash is embedded in the page and loaded correctly:
__flash__argumentstoxml is used to convert an argument to an XML expression __flash__arraytoxml used to convert an array to an XML __flash__escapexml to escape a word in XML __flash__ Objecttoxml is used to convert object to XML __flash__request to make up the request body __flash__toxml to convert data to XML
In fact, only string data can be passed between JavaScript and Flash programs, and any type of data is represented by the conversion of the above conversion function into an XML string. Assuming that the above method does not exist, or that the method above is incorrectly overwritten, it is likely that external JavaScript cannot invoke the interface to flash external registration.
Examples are as follows:
| 1 2 3 4 5 6 7 8 |
<script type= "Text/javascript" > var myarg = []; MYARG[10000] = 1; MYARG[10001] = {a:1, B: "string"}; </script> |
will be converted to (optimized):
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
< invoke name = "Test" ReturnType = "JavaScript" > < arguments > & nbsp < array > < property ID = "10000" > < number >1&L t;/number > </property > < property id = "10001" >   ; < OBJECT > < property id = "a" > & nbsp; < number >1</number > </Property > < property id = "B" > < string >string</string > </Property > </object > </Property; </array > </arguments > </invoke > |
JavaScript calls the Flash method back to the directoryCallFunction is the external calling interface of the Flash Player plug-in, which is a way to invoke flash from this interface, whether using C + + or JavaScript. __flash__request the incoming parameters into the final request body (XML above) and sends it to Flash Player, where Flash player gets the request-body resolution and invokes the Print method, passing in the request parameter and returning the execution result.
Examples are as follows:
| 1 2 3 |
<script type= "Text/javascript" > Alert (testswf.test); </script> |
Equivalent to the following:
| 1 2 3 4 5 6 7 8 |
<script type= "Text/javascript" > var arg = [' Test ']; for (var i=0; i<arguments.length; ++i) {Arg.push (arguments[i)); Testswf.callfunction (null , Arg)); </script><BR><BR> |
Flash calls JavaScript method
| 1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
var myjavascript:xml = <script> <! [cdata[function () {function sum (A, B) {alert (A + B); SUM (1, 2); }]]> </script> Externalinterface.call (myjavascript); Output 3<br><br> |
You can even inject JavaScript code:
| 1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
var myjavascript:xml = <script> <! [cdata[function () {window.sum = function (A, b) {alert (A + B); SUM (1, 2); }]]> </script> Externalinterface.call (myjavascript);<br><br> |
You can then perform the following calls:
| 1 2 3 |
<script type= "Text/javascript" > Alert (window.sum); </script> |
This can even embed your JavaScript code into flash, and the pros and cons of this approach are not discussed for the time being. However, Flash can not only invoke JavaScript methods in browsers or inject JavaScript code, it can even invoke and inject VBScript methods and code in IE.
method performance optimization back to directory
As mentioned earlier, Flash player will throw 6 functions to the global after successful SWF loading, however, unfortunately, these six global functions have varying degrees of performance problems, where the problem is more serious function is:
One of the functions: __flash__arraytoxml is responsible for converting the array to XML
| 1 2 3 4 5 6 7 8 9 |
function __flash__arraytoxml (obj) {var s = ' <array> '; for (var i=0 i<obj.length; i++) {s + = "<property id=\" "+ i +" \ ">" + __flash__toxml (obj[i) ) + "</property>"; return s+ "</array>"; } |
Suppose you convert the following JavaScript array to XML
| 1 2 |
var myarray = []; MYARRAY[10000] = 1; |
The above function loops 10,000 times, parsing all the array elements, whether they exist or not, into XML, which leads to the following problems:
Wasting performance on useless parsing makes the XML request incredibly large Flash player gets the XML data and then creates the XML DOM and uses the + + operation on the string, which is very inefficient in IE browser.
Function bis: __flash__escapexml is responsible for converting special characters to Entities