AJAX - The Complete Reference

(avery) #1

498 Part III: Advanced Topics


myXML.load(url);
}

static function main()
{
ExternalInterface.addCallback("connect", null, connect);
}
}

You should, after your inspection, notice a connect() method that takes a url and a
callback that is invoked upon success. This method has been exported to an included Web
page as indicated by the line ExternalInterface.addCallback("connect", null,
connect).
Now we need to convert this ActionScript into a Flash SWF file. Even if we don’t have
Flash, there are a number of ActionScript compilers on the Internet to do this. We compiled
the example using one called mtasc (www.mtasc.org):

mtasc -version 8 -header 1:1:1 -main -swf ajaxtcrflash.swf ajaxtcrflash.as

The 1:1:1 makes the SWF file a 1px × 1px running at 1-frame per second movie. Our goal
here is a bit unusual for Flash, to be invisible and behind the scenes to the user.
Next, we take our created SWF file and insert it into the page. The syntax to do this for
plug-in-focused browsers like Firefox is primarily using an <embed> tag like so:

<embed type="application/x-shockwave-flash" src="http://ajaxref.com/
ch10/flash/ajaxtcrflash.swf" width="1" height="1" id="helloexternal"
name="helloexternal" />

Microsoft’s ActiveX component technology would prefer to see the Flash specified like
so, using the <object> tag:

<object id="helloexternal" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
width="1" height="1" >
<param name="movie" value="http://ajaxref.com/ch10/flash/ajaxtcrflash.swf" />
</object>

Due to some unfortunate lawsuits regarding the use of binary objects within browsers,
we have to use script code to insert these elements lest we get a prompt to “Activate this
control” in the Microsoft browser. We create a simple function to do just that:

function createSWF()
{
var swfNode = "";
if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
swfNode = '<embed type="application/x-shockwave-flash" src=
"http://ajaxref.com/ch10/flash/ajaxtcrflash.swf" width="1" height="1"
id="helloexternal" name="helloexternal" />';
else { // PC IE
swfNode = '<object id="helloexternal" classid="clsid:D27CDB6E-AE6D-11cf-
96B8-444553540000" width="1" height="1" >';
swfNode += '<param name="movie" value="http://ajaxref.com/ch10/flash/
ajaxtcrflash.swf" />';
Free download pdf