A cleaner way of handling the Flash's fscommand within IE

Very recently I was debugging a piece of javascript with my colleague. I thought that the javascript didn’t look very clean, though it was copied from a reputed website. The javascript was basically meant for handling the fscommand event from a Flash movie and perform some actions.

The code to do this consisted of javascript as well as VBScript code, and involved document.write() which I found a bit odd. I thought that there had to be a cleaner way of doing things and soon found out this neat jscript code to accomplish the same. Let me tell you I am not an expert on Flash and the following code will work only with IE. Given below is an example flash movie and it’s actionscript code that calls the ‘fscommand()’ on mouse click and passed the coordinates of the point as parameters.

Click anywhere on the above flash movie

class Main
{
static function main()
{
_root.createTextField("t",2,15,10,200,40);
_root.t.text = "Click Here!";

_root.onMouseDown = function ():Void {
_root.t.text = _root._xmouse + ","+ _root._ymouse;
fscommand("MouseClicked",_root.t.text);
};
}

}

First we write a javascript function that we want to call on the fscommand. Then we write the code that links this function to the fscommand.

<object id="testflash"  classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#versionfiltered=7,0,0,0" align="middle"
style="width: 200px; height: 50px" VIEWASTEXT>
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="test.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffff00" />
</object>
<script language="javascript">
function ChangeText(x)
{document.getElementById("info").innerHTML ="You clicked here:"+x;}
</script>
<script FOR="testflash" EVENT="FSCommand" LANGUAGE="Jscript">
var count = arguments.length;
ChangeText(arguments[1]);
</script>

It is worth nothing here that the script is mixture of javascript and jscript. But at least it doesn’t contain document.write().