June 20, 2005
URL Strings Handled by JavaScript & Flash
One of the challenges that I face in my Flash development is the handling of dynamic choices in data selection in a not-so-dynamic environment. Because usage and access to databases and dynamic scripting requires an act of congress, I am left with the necessity of improvisation and taking advantage of as many client-side abilities as I can.
Challenge: I’ve got an XML file with an array of choices that the user can choose from. They’re basically flash-based marketing modules—animations with voice-overs about our various services and options. Each user, employee, or employer can choose which modules they want to view and that means that there could be hundreds of thousands of possible combinations of modules that people can view and in the order that they want.
Limitations: A static XML file containing data about the flash modules, HTML, JavaScript,
Solution: Well first thing’s first… figure out how to use JavaScript to parse a URL string. I will end up with a URL that will look something like this after the user has selected their modules:
http://www.mywebsite.com/modules.html?modules=1,2,3,4,5
Here is the JavaScript that grabs the value of module:
<script type="text/javascript">
<!--
var url = (location.href);
var numberArray = new Array();
var numberArray = url.split ("?modules=");
var showVariable = numberArray[1];
//–>
</script>
First we declare the variable “url” which has the value of (location.href), which is essentially the current address that the user is browsed at. From that we’re declaring an array called numberArray. We’re splitting up the variable url and creating the array “numberArray” and that array is being delimited by the value “?modules=”.
So for our example URL from above, the first array (numberArray[0]) would be this value:
http://www.mywebsite.com/modules.html”
The second array (numberArray[1]) would be this value:
“1,2,3,4,5″
I’m then taking that particular array and assigning it to this variable: showVariable. I’ll be using that with the flash plugin. That takes care of most of the JavaScript. Now we move onto the flash plugin code and then onto the Actionscript.
We’re using a little JavaScript to write the flash plugin code:
if (showVariable) {
document.write ("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='750' height='400' id='menuSystem' align='middle'> ");
document.write ("<param name='allowScriptAccess' value='sameDomain' /> ");
document.write ("<param name='movie' value='dynamicMenuSystem.swf?modules=" + showVariable + "' /> ");
document.write ("<param name='quality' value='high' /> ");
document.write ("<param name='bgcolor' value='#ffffff' /> ");
document.write ("<param name='FlashVars' value='modules=" + showVariable + "'>");
document.write ("<embed src='dynamicMenuSystem.swf?modules=" + showVariable + "' FlashVars='modules=" + showVariable + "' quality='high' bgcolor='#ffffff' width='750' height='400' name='menuSystem' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /> ");
document.write ("</object>");
}
There are two lines in particular you’ll want to notice:
document.write ("<param name='movie' value='dynamicMenuSystem.swf?modules=" + showVariable + "' /> ");
and
document.write ("<embed src='dynamicMenuSystem.swf?modules=" + showVariable + "' FlashVars='modules=" + showVariable + "' quality='high' bgcolor='#ffffff' width='750' height='400' name='menuSystem' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /> ");
Both of these lines take our variable “showVariable” (which contains the value of “modules” from the URL string) and here we tell the plugin to import the value of “showVariable” into a variable that we’ll be using in Flash called “modules”.
Now time for the Actionscript.
We’ve gotta take that variable and convert it to an array of numbers. From that array of numbers I’m able to selectively select which modules to display listed in my XML file. The Actionscript is rather basic and straight forward:
_root.selectArray = new Array(modules);
_root.selectArray = modules.split(",");
We’re taking the variable we imported into flash (modules) and have converted it to an array, which is split up (or seperated by) the comma (,). From that we get our array of numbers contained in the array name selectArray, which is located on the _root level.
DEMO: See the script in action: http://www.michaeltangen.com/flash/test_player.html?modules=1,2,3,4,5
Try changing the module numbers and the order that they’re in. The maximum number of modules goes up to 54 I think. Later I might show how I did the dynamic menuing system in Flash. That was another ordeal, but I managed to get it to work. Not bad for an actionscript amateur, eh?

June 20, 2005, 9:46 am
Filed under: Actionscript, Applications, Development, Flash (Macromedia), JavaScript
Leave a Comment
You must be logged in to post a comment.

