PDA

View Full Version : Cool: Inline Javascripts with ExternalInterface


Arodicus
03-03-2008, 07:16 PM
Perhaps this is already known, but I was really pleased to discover that the following experimental code actually works:

import flash.external.ExternalInterface;

var jsxml:XML =

<javascript>
<![CDATA[

function(){
alert("Does it work?");
alert("Heck yes it works!");
function subFunction() {
alert("Even this works!");
}
subFunction();
}

]]>

</CODE>;

ExternalInterface.call(jsxml.toString());

Tested in Firefox and IE.

This basically means you can store some VERY complex javascripts directly within Flash, with no special formatting or conversion into strings, by simply wrapping them up as XML and execute them in a browser... you don't even have to have them all in one line like you did with AS2. The trick is to use xml's <![CDATA[ ]]> construct, which causes the AS3 compiler to ignore the code, and to use an encapsulation function function(){} to gather all your code together into a single executable function.

Advantage? It means your Flash doesn't need a bunch of support scripts on the server - it can add it's own on-the-fly, which is very useful if you don't have complete control over your hosting environment.

ultraky
03-04-2008, 01:59 PM
groovy