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 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:

JavaScript:
  1. var so = new SWFObject("movie.swf", "flashMovie", "250", "150", "8", "#FFFFFF");
  2. so.addVariable("var1", getQueryParamValue("var1"));
  3. 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:

Actionscript:
  1. var sVar1:Object = ExternalInterface.call("getQueryParamValue", "var1");

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:

JavaScript:
  1. function getQueryParamValue(param){
  2. var q = document.location.search || document.location.hash; if(q){
  3. var startIndex = q.indexOf(param +"=");
  4. var endIndex = (q.indexOf("&", startIndex)> -1) ? q.indexOf("&", startIndex) : q.length;
  5. if (q.length> 1 && startIndex> -1) {
  6. return q.substring(q.indexOf("=", startIndex)+1, endIndex);
  7. }
  8. }
  9. return "";
  10. }

It's great for non flash websites as well.

Get example files

Get swfobject

23 Responses to “Best way to get address variables into Flash”

  1. clark slater Says:

    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

  2. guya Says:

    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.

  3. Dustin Says:

    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&lastName=Smith&location=Canada

    ////////
    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

  4. Dustin Says:

    (second half)

    for (var i=0; i

  5. Dustin Says:

    for (var i=0; i

  6. Dustin Says:

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

  7. 3PRIMATES Says:

    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

  8. 3PRIMATES Says:

  9. 3PRIMATES Says:

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

  10. robin Says:

    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

  11. Wendy Says:

    This works like a charm for me when I use it in a link from an html page:

    but when I try to use the link in a swf file:
    getURL(”player.html?chosen=x”);

    it can’t find the variable. Can anyone help me understand why not?

    Thanks,
    Wendy

  12. Wendy Says:

    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)

  13. link ekle Says:

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

  14. Passing Variables to Flash from HTML via Query String | codelinkers Says:

    [...] more @ http://blog.guya.net/2006/07/21/best-way-to-get-address-variables-into-flash/ var addthis_pub = ‘codelinkers’; Back to Codelinkers your virtual Link Sharing Blog [...]

  15. Anikrichard Says:

    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

  16. GUYA.NET » Blog Archive » Resolving some issues with swfobject Says:

    [...] The issue I had, was with the swfobject’s addVariable and addParam functions. The Flash SWF HTML seemed to be written to the page’s flashContent div but all of the variables and parameters I’ve added were ignored. After examining the swfobject getSWFHTML function, this function gives you the HTML code that is gonna embed the Flash inside the page, when I saw how strange the HTML is, I realized what happened: Without naming names some Javascript developers, extensions and frameworks like to write to the prototype of generic Javascript objects (This is also how Object Oriented Actionscript 1 was done in the past). And with doing so, extending these built-in objects (object, array, string, etc’) with various functionalities. A good example is the javascript JSON implementation which extends the Javascript object with object.toJSONString(). Swfobject stores the variables and parameters inside a regular Javascript object and when it prepares the Flash HTML it uses a for..in loop to go through all the elements and add them to the markup <param value=”flashMovie.swf” name=”movie” /> <param value=”transparent” name=”wmode” /> etc’ [...]

  17. GUYA.NET » Blog Archive » Understanding Flash’s ExternalInterface. Says:

    [...] Best way to get browser’s address variables into Flash. Controlling another Active-X control - creating flash buttons for Windows Media Player or Apple Quicktime. Putting Javascript power in Flash’s use - RegEx and XPath. Catching Javascript Exceptions in Flash. Creating, a, dynamic Flash website with AJAX. [...]

  18. val Says:

    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

  19. guya Says:

    @val,

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

  20. neeraj Says:

    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.

  21. dtk1864 Says:

    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?

  22. omaha poker online spiele Says:

    poker online mac…

    Atoutprix kasino spiel holdem poker eve online rig slots computer poker game spiel lobby…

  23. ??????????? ??? Says:

    ??????????? ??? ????????? ??????
    ???????????? ??????.
    ???????? ?????????? ??? ????? ??????????). (???????????
    ????????? ????????? (??????????? ?????, ????? ???????????? ???????????, ????? ??????????? ???????????).
    ??????????????? ??????? ?? ???????????.
    ??????????????? ??????? ?? ????? ????????????? ??????????.
    ??????????? ?????????? ??????????? ?????????? (1 ???????).
    ??????????? ?????????? ????????? ???? ??? ????.
    ????????????? (??? ???????????? ?????????????) ?????? ??????????? ?????.
    ?? ?????? ?????? ????????? ???????? ??? ? ??? ???? ? ???????? ?????????? ???????????? ?? ???????? ??????????? ?????? ???.
    ????????? ??????????????? ?????? ?? ??????????? ??? ?? ??? ?????? ??????, ?? ????? ?????, ??? ???? ??????????? ?????????? ???? ?????. ???
    ????? ?????????? (???????? ???? ??? ???????? ?? ?????? ???????? ? ??? ?? ?????? ????? : ??? ?????????? ?????? ?????? on-line, ????? ????????????? ??? ?????????? ?????????? ? ?????)
    ??? ??????????? ??? ?????:
    ??????????? ???????? ? ???????????? ???????????????? ????? ???? ???????? ??, ??????????? ????????, ? ????? ????.
    ??????????? ??????????? ??? ????? ???? ? ???? ????, ??????? ????? ?????? ????????? ??????????????? ??????? ??????.
    ?????????? ?????????? ???????? ?? ?????? ????????? 50 ???????. ????? ???????? ?? ????? ????? ????? ???????? ????????? ????????????? ?????? ??????????? ????, ????????? ?? ?????? ?????????.
    ??????????? ???????? ??????? ??? ?????????? ????? 10 ?????? ? ?? ?????????? ????????. ??????? ???????? ? ??????? ???????? ????? ???? ??????, ?????? ??????, ?????????, ????????????? ?????, ??????? ????????
    ??? ??????. ?????? ????????? ???????? ??????????, ????????? ???????? ????????? 20 ????? ??????, ????????? ??? ???????????? ????????. ????????? ???????? ?? ???????? ?? ??? ?????????????? ????? ? ???? ???????, ????????? ? ????????????? ???, ? ???????? ????????? ????????? ???????. ???
    ????????? ????? ????????, ??????? ?????? ????????? ??? ???????? ? ????? ???????????? ???????????, ? ????? ? ????? ????????????? ? ???????? ????????. ????????? ?? ?????????? ? ?????? ???????? ????? ?????????????? ???????? ? ?????, ?????????????? ??????????????? ??????????? ??????????? ???.
    ???????????? ?????????? ??? ??????????? ??? ????????? ??????????? ?????????? ????????????? ? ?? ????????? ??????? ????.
    ??????????? ??????? ??????????????? ??? ?? ??????????? ?????? ????????? ? ??????? ????? ?????????????.
    ? ????? ????? ???????? ???????? ??????????????? ????????, ??? ??????????? ???????? ???? ????? ??? ??????????? ?????. ?? ???????????, ????????? ???????????? ?? ???? ???? ?? ?????????????? ???????, ? ???????? ??????????? ???????.
    ?????? ?????????? ??????????????? ??????????? ??? ???? ?????? ?????? ??????????? ??????????? ?????????????, ???????? ? - ????????????? ????????????? ?????? ??????? ?? ???? ??? ??????.

Leave a Reply