Let me tell you about another Flash Player ExternalInterface
bug I encountered recently while working on the YUI Charts. When passing an Object from ActionScript to JavaScript, make sure your variable names don’t require quotes. Let me explain with a quick example.
This is legal ActionScript, but the variable name requires quotes because it has a hyphen:
var test:Object = { "legal-name" : "hello!" }; //just fine!
This code will throw a compiler error:
var test:Object = { legal-name: "hello!" }; //error!
Now, let’s look at a quick little class I built.
package { import flash.display.Sprite; import flash.external.ExternalInterface; public class ExternalInterfaceVariableNames extends Sprite { public function ExternalInterfaceVariableNames() { var test:Object = { "legal-name": "hello!" }; ExternalInterface.call("fromFlash", test); } } }
The HTML document in which we’ll embed this SWF will have the following simple JavaScript function:
<script type="text/javascript"> function fromFlash(value) { console.log(value); } </script>
Note: The function console.log()
is used to display text in the Firebug console a lot like trace()
is used in ActionScript to debug in Flash authoring or Flex Builder.
When I try to run my simple SWF, the following error is displayed in the Firebug console:
missing : after property id [Break on this error] try { __flash__toXML(fromFlash(({legal-name:"hello!"}))) ; } catch (e) { "What exactly is that showing us? Basically, for ExternalInterface to work, Flash Player places some code into the document. The important part of the code in the error is this:
{legal-name:"hello!"}Look familiar? Just like ActionScript, JavaScript requires variable names with hyphens to be quoted. Bug confirmed. Of course, I'm submitting this bug to Adobe so that it can be fixed in a future version. Make sure you always report bugs you find in Flash Player, and try to include test cases whenever possible!
this isnt really a bug IMO
Its a feature of the language in which you cannot use “-” in variable names as the compiler thinks its a minus sign
That’s why you can put quotes around it. Another example would be language keywords (though they pass through ExternalInterface just fine because JavaScript is less strict).
myVar.class
leads to a compiler error butmyVar["class"]
doesn’t.Imagine if you simply couldn’t parse XHTML files with E4X because the
class
attribute throws errors. The ability to use string access for variable names is an important “feature of the language”.