Publishers of technology books, eBooks, and videos for creative people

Home > Blogs > Using Flash Variables in Flex 4

Using Flash Variables in Flex 4

Flash applications can be visually integrated into an HTML page, but they're still technically removed from the rest of the browser. And Flash applications are even further removed from the server, where much of a site's true functionality and dynamism are found. But by using Flash variables, abbreviated flashvars, you can easily bridge these gaps.

To understand how to best use flashvars, you'll need to grasp how Flash content is run within a Web browser. A Web browser, of course, just understands HTML, JavaScript, and CSS; the browser on its own cannot recognize Flash. So to add Flash content to an HTML page, the <object> or <embed> HTML tags are used. To ensure that the page degrades nicely, JavaScript is normally invoked to create and manipulate these tags. Back in the day, a special JavaScript function called AC_FL_RunContent() handled the creation of the HTML, but now the SWFObject is the preferred agent (the SWFObject is hosted at Google Code).

In the most current version of Flex, then, the code for placing a Flash application, which is a SWF file, within a Web page looks partly like this:

"Main.swf", "flashContent",
"100%", "100%",
"10.0.0", "playerProductInstall.swf",
flashvars, params, attributes);

The reference to the SWF file is really the most important argument to the embedSWF() function, and you can check out the SWFObject documentation for the others, but I want to focus on the flashvars argument now.

The flashvars argument is simply a way to pass name=value pairs to the SWF file. In the above code, flashvars is assumed to be a JavaScript object. For example, the next few lines of JavaScript store information about the user's browser and computer in three properties of the flashvars object:

var flashvars = {};
flashvars.browserName = navigator.appName;
flashvars.browserVersion = navigator.appVersion;
flashvars.platform = navigator.platform;

Now if this flashvars variable gets passed to the swfobject.embedSWF() function, then the Flash application itself will be able to access that information. To do so, the ActionScript in the Flex code would refer to Application.application.parameters. That property will be equivalent to the original flashvars object. So Application.application.parameters.browserName would have the value of navigator.platform (e.g., MacIntel).

Knowing just this is useful, but not where you want to stop. Presumably there will be information you'd like to pass to a Flash application that's not available in JavaScript. For example, you may have server data that should be available to the Flash application. This could just be some basic information, like passing along a product ID or some other relevant signifier. Or perhaps you want to provide to the Flash application something stored in a session. You could use Flash to perform an HTTP or other request of the server, but if you're already using a server-side technology to generate the HTML page, why not have it write out the requisite JavaScript that will in turn get passed along to the Flash application. For example, if you're using PHP to generate an HTML page that displays some Flash, you could have PHP do the following (in the right place in the HTML code, that is):

flashvars.username = '{$_SESSION['username']}';
flashvars.language = '{$_SESSION['language']}'; = {$_GET['fid']};" ?>

The above code assumes that the user has already logged in and arrived at this page. Three pieces of already gathered information will then be passed to the Flash application. This is really simple to do, you just need to watch your syntax so that the outputted JavaScript is syntactically valid.

There are two key security considerations when doing this, however. First, the source code of any HTML page is visible to the end user, including the JavaScript, so you don't want to write anything of a sensitive nature into the code. Second, the HTML source of a page and the flashvars sent to the JavaScript function can easily be hacked, so you'll want to make sure that the Flash application is sanctifying the received flashvars before doing anything potentially hazardous.