How to Pass Dynamic URL Variables to a SWF using ColdFusion

Social Share Toolbar

Whew, It’s been a LONG time since I posted anything! Lucky I don’t have a big following.

I recently found myself in a situation where I needed a way to pass certain URL variables to a SWF within a ColdFusion page. This may sound easy on the surface but the catch is that the page is dynamic. It never knows which SWF it’s going to call and each SWF can take different URL variables. I ended up with a pretty clean solution that didn’t take many lines of code. Note that I use SWFObject to place my Flash objects (and so should you) and this solution is specific to that method.

I send one URL variable called customvars that contains all of the variable names and values I want to send to the SWF. Variable names and values are separated by commas and each name/value ‘set’ is separated by a semi-colon. It ends up looking like this:

customvars=user_id,123456;eyecolor,blue;car,accord

Then, in my CF page I put the following code which converts the customvars string into a CF structure:

<cfset flashVars = structNew() />
<cfparam name=”url.customvars” default=”">
<cfif len(url.customvars)>
<cfloop list=”#url.customvars#” delimiters=”;” index=”customFlashVar”>
<cfset thisRecordName = listFirst(customFlashVar,”,”)>
<cfset thisRecordValue = listLast(customFlashVar,”,”)>
<cfset flashVars[thisRecordName] = thisRecordValue>
</cfloop>
</cfif>

Then, in my swfObject code, I loop through the structure assigning the name and variable to swfObject’s addVariables like this:

<cfloop collection=”#flashVars#” item=”key”>
fo.addVariable(“#key#”,”#attributes.parms[key]#”);
</cfloop>

Works like a charm.

3 thoughts on “How to Pass Dynamic URL Variables to a SWF using ColdFusion

  1. Hi James,

    Thanks for your post.

    ColdFusion can reference URL variables. As a matter of fact, I’m using it in the post:
    list=”#url.customvars#”

    My post is demonstrating how to pass multiple variables under one url variable, parse through them, and then pass them to a Flash application as separate variables.