Passing parameters into a Flex 4 (Beta 2) application is slightly different to passing parameters into a Flex 3 application. Previously you would pass a string into the flashvars parameter with the key/value pairs, now you pass in an associative array with the key/value pairs.
In the launcher HTML template file you will see the following javascript:
var swfVersionStr = "${version_major}.${version_minor}.${version_revision}";
var xiSwfUrlStr = "${expressInstallSwf}";
var flashvars = {};
var params = {};
params.quality = "high";
params.bgcolor = "${bgcolor}";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "${application}";
attributes.name = "${application}";
attributes.align = "middle";
swfobject.embedSWF(
"${swf}.swf", "flashContent",
"${width}", "${height}",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
swfobject.createCSS("#flashContent", "display:block;text-align:left;");
As you can see the flashvars and params are now passed into the Flash object as associative arrays, and the method that is called on the Flash object is no longer AC_FL_RunContent().
To get the query string into the flashvars object is very easy, simply iterate through the query string and add each key/value to the flashvars object.
var swfVersionStr = "${version_major}.${version_minor}.${version_revision}";
var xiSwfUrlStr = "${expressInstallSwf}";
var flashvars = {};
var queryString = window.location.search.substring(1);
if (queryString != null && queryString.length > 0)
{
var keyValuePairs = queryString.split("&");
for (var i = 0; i < keyValuePairs.length; i++)
{
var keyValuePair = keyValuePairs[i].split("=");
if (keyValuePair.length == 2)
{
flashvars[keyValuePair[0]] = keyValuePair[1];
}
}
}
var params = {};
params.quality = "high";
params.bgcolor = "${bgcolor}";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "${application}";
attributes.name = "${application}";
attributes.align = "middle";
swfobject.embedSWF(
"${swf}.swf", "flashContent",
"${width}", "${height}",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
swfobject.createCSS("#flashContent", "display:block;text-align:left;");
Then to access the parameters inside your Flex application you just need to access the parameters property on the application object.
var someVariable:String = this.parameters["MyQueryStringKey"]; // OR var someOtherVariable:String = FlexGlobals.topLevelApplication.parameters["MyQueryStringKey"];
That’s it, simple
Post based on Adobe Flash Builder 4 Beta 2
Comments