AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 499


swfNode += "</object>";
}
/* put the Flash reference in the page */
document.getElementById("flashHolder").innerHTML = swfNode;
}

NNOT EOTE Insertion and manipulation of Flash movies is filled with all sorts of little details. Many
developers rely on scripts like SWFObject (http://blog.deconcept.com/swfobject/) to perform such
tasks. Our point here is demonstration, and the approach taken should work for most readers.

Once our communications SWF file is inserted into the page, we find the Flash movie
and then use its externally exposed connect() method to make our call to a URL and
specify the callback we want to use. Of course, nothing can be the same between the
browsers. We see accessing the SWF object is a bit different, so we write a little function to
abstract that as well:

function getSWF(movieName)
{
if (navigator.appName.indexOf("Microsoft")!= -1)
return window[movieName];
else
return document[movieName];
}
flashBridge = getSWF("helloexternal");

Finally, after getting a handle to the Flash object we issue the request:

flashBridge.connect("http://unsecure.ajaxref.com/ch1/sayhello.php ",
"printMessage");

This will later call printMessage and show us content from another domain! Figure 10-4
shows the demo at http://ajaxref.com/ch10/flashajax.html breaking the same origin policy.
The complete code that enabled this is shown next for your perusal.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chapter 10 - Breaking SOP with Flash</title>
<script type="text/javascript">
function createSWF()
{
var swfNode = "";
if (navigator.plugins && navigator.mimeTypes && navigator
.mimeTypes.length)
swfNode = '<embed type="application/x-shockwave-flash"
src="http://ajaxref.com/ch10/ajaxtcrflash.swf" width="1" height="1"
id="helloexternal" name="helloexternal" />';
else {
swfNode = '<object id="helloexternal" classid=
"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1" height="1" >';
swfNode += '<param name="movie" value=
Free download pdf