Skip to content
All posts
·2 min read

Best way to get address variables into Flash

I’ve seen too many wrong or old soulotions for sending query string variables into Flash (page.html?var1=Jon&var2=Smith… etc.). In fact, if you’ll google it…

I've seen too many wrong or old soulotions for sending query string variables into Flash (page.html?var1=Jon&var2=Smith… etc.). In fact, if you'll google it you'll find this old Macromedia article from the year 2000 and about Flash 4, while most of it still applies it's definitely old. These days, we're using swfobject, if you don't use it then you should, swfobject comes with a "global" javascript function called getQueryParamValue and you can use it for setting query/address string variables into FlashVars as follows:

var so = new SWFObject("movie.swf", "flashMovie", "250", "150", "8", "#FFFFFF");  
 so.addVariable("var1", getQueryParamValue("var1"));  
 so.addVariable("var2", getQueryParamValue("var2"));

These variables will be availble as _root.var1, _root.var2, etc. For complete explanation go here. The best thing about FlashVars is that they are available for use as soon as the Flash movie starts, and before everything else, so you can refer, for example, to the variable _root.var1 in the first line of code, as opposed to another old method setVariable, where we had to wait for the variable availability using somthing like watch.

Though the advantage of FlashVars, personly, I don't like _root variables, it seems unclean, especially when it's a long query string that'll make tons of them floating in my _root. Since Flash 8 we can use ExternalInterface to call to ***getQueryParamValue ***and get an immediate and synchronous response from within Flash, that way we can have a neater control over owr address variables, for example:

[as]var sVar1:Object = ExternalInterface.call("getQueryParamValue", "var1");[/as]

Play with the example, try different variables.

If you don't use swfobject, I guess you can easily write your own javascript method for getting address variables, but why bother, I don't think you can easily improve on that one. I also don't think that Geoff Stearns, the author of swfobject, will care if you'll copy this function:

function getQueryParamValue(param)\{  
 var q = document.location.search || document.location.hash; if(q)\{  
 var startIndex = q.indexOf(param +"=");  
 var endIndex = (q.indexOf("&", startIndex) > -1) ? q.indexOf("&", startIndex) : q.length;  
 if (q.length > 1 && startIndex > -1) \{  
 return q.substring(q.indexOf("=", startIndex)+1, endIndex);  
 \}  
 \}  
 return "";  
 \}

It's great for non flash websites as well.

Get example files

Get swfobject

Comments (20)

Imported from the original blog

clark slaterJul 21, 2006

Wow strange coincidence you wrote this entry today. I have just finished up a project that uses remoting for .NET and strangely I found that passing variables into Flash with all the methods shown below actually breaks Flash Remoting for .NET. Oddly, AMFPHP was not affected by this!

Can't see why, but my service can't be found when I write even a single variable to the main timeline at instantiation. My remoting code is in a separate class and nothing to do with the main timeline. I was calling a .NET assembly as my service.

Eventually, I had to resort to a getConfig method that could be called with ExternalInterface to retrieve runtime variables.

I'm wondering if this is related to Flash Player 9, must roll back to FP8 and see if the same problem exists.

- appending to the swf
e.g. myfile.swf?var1=x&var2=y

- using flashvars with swfobject
e.g. so.addParam ("flashvars", ""var1=x&var2=y")

- using addVariable with swfobject
e.g. so.addVariable ("var1", "x")

Clark

guyaJul 22, 2006

Strange indeed, what is also strage is the issue you're describing there, it's good to know that such an issue might happen, and I'm glad you found a solution.

DustinOct 17, 2006

Hey Guya,

Thanks for the great article.

I need to have my flash grab multiple vars from the URL. At first I was using multiple ExternalInterface calls but it seemed like a waste of code, objects and javascript calls. I have tweaked the getQueryParamValue code to loop through the adress and return objects for each variable. These can then be applied as parameters to the object doing the external interface call.

////////
The URL:

domain.com?firstName=John&l...

////////
JS:

getRequestParameter: function() {
var q = document.location.search || document.location.hash;
if(q) {
var parameter_array = new Object();
var pairs = q.substring(1).split("&");

// I have moved the length check outside of the for loop, this increases the efficiency because it doesnt have to check the length of the string per loop
var pairs_length = pairs.length;

for (var i=0; i

DustinOct 17, 2006

(second half)

for (var i=0; i

DustinOct 17, 2006



for (var i=0; i

DustinOct 17, 2006

uh.. sorry for wrecking your blog the less than sign seems to break it.

3PRIMATESOct 29, 2006

Hi, I have always like this siimple little script for getting my vars from a url.
I use this with swfObject and pass the vars I need to it.

function getVars(key_str) {

if(window.location.search) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i

3PRIMATESOct 29, 2006

Ok, sorry, doesnt seem to work. How do you post code in a reply?

robinNov 17, 2006

Im creating a viral card in flash and trying to work with programmers to add the swfobject.js code and the getQueryParamValue function. Basically the url is: http://site/index.jhtml?name=robin&x=10&y=12 and the flash is created with a dynamic text box to load the "name" variable. I understand the js needed but what actionscript is required for the flash, if any? thanks for the help

WendyNov 27, 2006

Hmmm, it posted the html link as html, and I just tried to use entities for the brackets, but it still didn't go through...
so what I use in the html link is

(bracket)a href="player.html?chosen=x"(bracket)

link ekleMar 23, 2007

In the example above, the page header and intro text prints. I’m trying to suppress that as well.

AnikrichardSep 5, 2007

hello , my name is Richard and I know you get a lot of spammy comments ,
I can help you with this problem . I know a lot of spammers and I will ask them not to post on your site. It will reduce the volume of spam by 30-50% .In return Id like to ask you to put a link to my site on the index page of your site. The link will be small and your visitors will hardly notice it , its just done for higher rankings in search engines. Contact me icq 454528835 or write me tedirectory(at)yahoo.com , i will give you my site url and you will give me yours if you are interested. thank you

valJun 1, 2008

hi! people

i am trying to parse a variable to flash via php with this swfobject and after 2days i am still not able to even if i just ran accross this article lately yesterday

here what i am trying to do

i have my first page listvideo.php?vid=x sending that id vid=x to watch.php and that's where i put my swf with all ur following explanations...

it works when i just pass the variable directly from watch.php to send it to the player.swf but when i use the listvideo.php to send the vix=x to watch.php and so watch.php could past it to player.swf it doesn't work at all can somebody please help me and tell me what did i do wrong

i am trying to parse video name to player. thank you

guyaJun 1, 2008

@val,

How do you send the 'vid' parameter from listvideo.php to watch.php?
Do you have a link where it can be examined?

neerajJun 2, 2008

hii, i hav a webpage in 10 atist names r there,when i click on one artist name it hides then Div frame & show it painting in slideshow using flashvars.

can we change flashvars value using javascript without refreshing or reloading pageusing external interface.

Is it possible.

dtk1864Jun 8, 2008

I can't seem to find out how I can store a variable in one swf and have another swf reference the same variable. I have animated menu's that archive swf files, and for ease of navigation I have swf files from a previous year return to that section of the separate swf menu. I'd like to be able to set a variable (ie: year = 2006) in the swf file, that the separate swf menu can evaluate and view the scene/label associated with 2006 instead of 2008. Right now I have made multiple (identical) menu's each starting with a corresponding year, but this seems to bog down my web with unnecessary swf and html files.

Does flashplayer reset values when it loads a new swf? or am I scripting something wrong?

werutzbOct 8, 2008

Hi!

I would like extend my SQL capabilities.
I red really many SQL resources and would like to
get more about SQL for my position as mysql database manager.

What would you recommend?

Thanks,
Werutz

marc herfurthMar 11, 2009

@dtk1864 & all others who might be interested in
"store a variable in one swf and have another swf reference the same variable":
you might be interested in flash's SharedObject

Internet BankingFeb 4, 2010

Strange this post is totaly unrelated to what I was searching google for, but it was listed on the first page. I guess your doing something right if Google likes you enough to put you on the first page of a non related search. :)