Flash and database communication Part 1: Using URL Variables and FlashVars

Posted February 14, 2005 10:56 pm
Filed under: ActionScript, Articles by Paul, Flash, Tutorials

Note: This article is the first in a series which describes how to connect a Flash project with a database, or more generally, how to pull external data into a .swf file playing in the Flash Player. The series introduction contains a list of all the articles.

If you’d like, you can:
Download the example files
Skip to the summary at the end

The first technique we will look at is the easiest to implement, but also the most limited in terms of usefulness. This approach to getting data into the Flash project is directly tied to the HTML source code that is used to “include” the .swf file in the containing web page (these instructions assume you are using this technique to pass values into a Flash movie that is contained in a web page — otherwise this approach doesn’t really apply).

Review: How a Flash Player movie (.swf file) is “embedded” into a web page.

Because the HTML source code that activates the Flash Player is so important to this approach, I will take a moment to review what it looks like and how it works. If you feel comfortable with this topic already, feel free to skip down to the next section.

In general, to embed a .swf file in a web page two HTML tags are used. You can see this by creating a new Flash project and choosing File > Publish to create a .swf file and a basic HTML page container (the default Publish Settings are to create both a .swf file and a web page). Once you have published the movie, you can view the source code for the container web page. Using the standard templatenote 1 that comes with Flash MX 2004, the portion of the code that specifies to include the .swf file will look something like this:

.
.
.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="550" height="400" id="URLvariables" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="URLvariables.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="URLvariables.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="URLvariables" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
.
.
.

As mentioned, there are two tags, the <object> tag (and it’s child <param> tags) which are used by Internet Explorer (Windows), and the <embed> tag which is used by all other browsersnote 2. You can see that each of these tags specify several values that control the behavior of the Flash Player, such as the URL of the .swf file, the background color, the playback quality, etc. In general, these correspond to options that are selected in the Publish Settings… dialog box. The most important thing to note is that, since there are actually two separate tags which perform the job of activating the Flash Player, each value appears twice, usually in a <param> tag for the options associated with the <object> tag and as attributes within the <embed> tag.

URL Variable Format

If you have done any dynamic web page programming or have paid some attention to the way web browsers work, you have probably noticed that in some cases, for example when you submit the search form on Google, certain variable values are passed to the web server as part the URL. The variables appear at the end of the URL, like this:
http://www.example.com/index.php?var1=value1&var2=value2

A question mark designates the beginning of the variables. Next each variable and its value appear as a pair with an equals sign. Variable/value pairs are separated by the ampersand (&) character. This format is often referred to as URL variable format or querystring format. In this format most characters other than regular latin alphabet characters must be encoded in a special format (known as “URL encoding”) in order for them to be passed correctly. Most web programming systems include some command for doing this (e.g. Server.UrlEncode() in ASP/ASP.NET; urlencode() in PHP).

Passing values to the .swf file

Just as URL variables can be appended to a URL to pass values to a web page on a server, they can be used in just the same way to pass values into a .swf file in the Flash Player. When you add a URL variable sequence to the end of the .swf filename which is passed to the Flash Player (in the “movie” <param> tag or in the <embed> tag’s “src” attribute), variables with the specified names will be created and assigned the specified values. Those variables will be created on the main timeline of the Flash project, and will be available to any ActionScript on that timeline (or using _root.variableName).

For example, the sample file URLVariables.fla has a single dynamic text field on the stage, with an instance name of myTextField_txt. There is a single line of ActionScript on the first frame which reads:

myTextField_txt.text = var1;

In the source code for the companion HTML file, URLVariables.html, a URL variable sequence has been added to the end of the .swf file’s name with a single variable named “var1”, which is given the text value “value1”. The important lines of code look like this:

...
<param name="movie" value="URLvariables.swf?var1=value" />
...
<embed src="URLvariables.swf?var1=value" quality="high" bgcolor="#ffffff" width="550" height="400" name="URLvariables" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
...

If you open this file in a browser, you will see the text “value1” in the text field, because the variable “var1” is created in the Flash project before the line of ActionScript is executed.

If you test this Flash project within Flash, you will see the text “undefined” in the text field, since the code assigns the value of the variable var1 (which has no value if the .swf file isn’t playing in a browser, hence “undefined”) to the text property of the text field.

Things to keep in mind

Obviously there isn’t much utility in having the variables/values hard-coded in a static HTML page. To make this approach useful, you would use some sort of dynamic web page language to come up with some dynamic data, whether it is pulled out of a database, passed in through a form, etc. You would then code the page so that value given to the variable is dynamically written into the HTML source before returning it to the user — just as you would dynamically write any other content to the page. A working example of this is available in the example files as URLVariablesDynamic.fla, with its accompanying .swf file. You can try out a live demo of this project, and the PHP (and equivalent ASP.NET) files are also included in the example files.

However, for testing purposes it is common (and useful) to just use a static HTML page with the values hard-codednote 4. That way you can test your Flash project and get it working as desired first, then move on to coding the dynamic page portion. By testing the page with static data you can isolate any errors that happen during testing as being due to an error in the Flash project (and conversely, if things do work you will know that they should still be working when you hook it up with dynamic data).

Important limitation

One big limitation applies to this way of passing data into Flash. Normally, web browsers cache .swf files and will not generally download a new version of the .swf file. However, when you append URL variables to the .swf file’s URL, the general behavior is that web browsers will download a new copy of the .swf file, even if the .swf file itself hasn’t changed and only the text of the URL variable string has changed.

In most cases this is bad, because it means that users will be forced to re-download the same .swf file every time a variable’s value changes (even if nothing has changed in the .swf file itself). It can actually be beneficial if you want to force users to download a newer version of a .swf file (normally browsers won’t download the newer version even if the .swf file has changed). By appending a URL variable such as a version number, and changing that value in the HTML code every time you change the .swf file, you can make it much more likely that users will always see the current version of the .swf file. As long as the version number hasn’t changed, the user’s browser will generally use the copy in the cache, saving download time on return visits.

The next iteration: FlashVars

Macromedia recognized that the URL variables approach to loading data really isn’t acceptable, since it prevents browsers from using the cached copy of a .swf file. Consequently, with the release of Flash MX and the Flash Player version 6, they added support for a new construct known as FlashVars which can be used to accomplish the same task of passing in variables when a .swf file loads, but without the unwanted side effect of forcing the browser to download the .swf file again.

FlashVars works in a very similar way to the URL variables method of passing data into the Flash project. However, instead of appending the variables and values directly to the .swf filename, they are included as a separate parameter of the <object> and <embed> tags. To pass variables in using the FlashVars approach, add an extra <param> tag and an extra attribute to the <embed> tag. For the <param> tag the “name” attribute should be “flashvars” and the value should be the list of variable-and-value pairs, still in URL variable format (i.e. separated by ampersands, URL encoded, etc.). The <embed> tag’s new attribute is named “flashvars” and has the variable/value pairs as its value, in the same format.

Here is the same HTML code that we looked at before, modified to use FlashVars instead of URL variables:

.
.
.
<param name="movie" value="URLvariables.swf" />
<param name="flashvars" value="var1=value" />
.
.
.
<embed src="URLvariables.swf" flashvars="var1=value" quality="high" bgcolor="#ffffff" width="550" height="400" name="URLvariables" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

.
.
.

You can see this code in context in the sample file FlashVars.html. Other than not forcing the browser to reload the .swf file, this approach works just the same from the Flash/ActionScript perspective (in fact, the example file uses the same .swf).

Complex/Large amounts of data

Admittedly, all the examples I’ve shown have been pretty simple overall. In general this approach isn’t the best to use if you want to bring in a large amount of data or data that is fairly complex. However, I will talk about one way that I have used to do this.

Suppose you want to create a Flash project that will be a generic graphing tool — you will feed it a set of labels and values, and it will create a bar chart of that data. The trick, of course, is that you don’t know exactly how many items will be passed in. Ultimately we will want the data to be in the Flash movie as multiple arrays, perhaps one which contains the values for the chart, and one which contains the labels associated with those values. One way to pass in one or more “arrays” of data using FlashVars is to have a variable representing each array you want to pass in, and then have the value assigned to that variable be a comma-separated list of values (or some other character that you know won’t be found in the data itself).

So for our charting example, we could have two variables, one named “labels” and one named “values”. The “value” of our FlashVars <param> tag/attribute in the HTML document would look something like this:

labelstring=hamburger,cheeseburger,chicken&valuestring=5,3,6

So when our movie loads, there would be two variables available to us, one named labelstring and one named valuestring. According to the Flash Player these would be String variables, so we would need to create our own Array objects out of those variables. That is easy enough to do using the split() method of the String class. To create our arrays, we would use these lines of ActionScript:

var labels:Array = labelstring.split(",");
var values:Array = valuestring.split(",");

We could then use those array variables to populate our chart or for whatever purpose we wanted to use them.

Summary

Here is a review of what we’ve covered in this article.

How to get the data into Flash:

  1. Choose the approach to use:
    • URL Variables: append variables and values to the .swf file name in the HTML source code of the page (this usually means they have to be added in two places). The variables should be in a “URL Variable” format, like “mymovie.swf?var1=value1&var2=value2”.
    • FlashVars: add an extra <param> tag and an extra attribute to the <embed> tag. The <param> tag’s name should be “flashvars” and value should be the list of variable and value pairs in URL variable format (no question mark is needed in this case). The <embed> attribute should be named “flashvars” and its value should be the list of variables in the specified format.
  2. In either case, when the .swf file loads in the Flash Player, the specified variables will be available as variables on the main timeline, with the given values.

Strengths:

Limitations:

Notes

Note 1: There are several web page templates that are included with Flash. You can choose the one you want to use in the File > Publish Settings… dialog box. Under the HTML tab the Template pull-down menu can be used choose from several HTML page templates, depending on whether you need certain features in your Flash project. You can also create your own templates or customize the ones that come with Flash, as described in Flash Help > Using Flash > Publishing > About HTML publishing templates and the topics under Customizing HTML publishing templates.

Note 2: As you probably know if you are interested in Flash and in Web standards, the default HTML code in the templates that come with Flash is not valid XHTML, in spite of the fact that it includes an XHTML 1.0 Transitional doctype declaration. Consequently, including that code in a page will cause that page to not validate. An alternative approach is discussed in an A List Apart article on the subject; there are others as well (most notably using JavaScript to programmatically write the HTML code to the page).

Note 3: If you are like me and are a stickler about always declaring variables in your Flash projects, you can still declare them (as String variables). Since the Flash Player just ignores variable declaration statements for variables that are already defined, it won’t overwrite the value that was passed in the URL.

Note 4: Once you have published the movie the first time to create the HTML page, and edited the HTML source code to include the variables, be sure that you do not publish the Flash movie again without un-checking the option to publish an HTML file. If you publish a new HTML file, the file with the variables added in will be overwritten and the variables will be lost. This is only an issue if you “compile” the Flash project using File > Publish or File > Publish Preview; if you test the movie to create a new .swf file a new HTML page will not be generated.

You can leave a comment, or trackback from your own site.

42 Comments so far


  1. Flashvars is reported to have said:

    You might be interested in this javascript function that will write out the code to place the flash object complete with flashvars -
    http://www.centralquestion.com/archives/2005/03/writeflash_flas.html


  2. Tara is reported to have said:

    how can i change a value of a variable into a variable in flash through which i can put another value in the variable(which is a value changed into a variable).

    please reply me…..

    best regards Tara

    swain_tara@yahoo.co.in
    tara.prasad@yahoo.com


  3. Jonathan is reported to have said:

    Is there any way this can be done without the embed tag? Refering to the W3C you can use this and have valid pages.


  4. Paul is reported to have said:

    Jonathan:
    There are a couple of ways to work around using the embed tag (or more specifically, to allow the pages to validate with W3C), although they each have their shortcomings.

    One that is probably the more popular one is to use JavaScript to write the object and embed tags into the document — technically these approaches usually still use the embed tag, but it’s being written into the page dynamically so it’s not in the markup, which is what the validator cares about.

    Another side benefit of this approach is that it gets around the new “click to activate content” issue that Microsoft added to the updated IE6 (and the forthcoming IE7) in order to avoid side effects of the Eolas patent.

    Finally, these approaches often incorporate other features, such as Flash Player version detection and automatic upgrading where possible.

    Here are two solutions that use the JavaScript approach:

    SWFObject

    Adobe Active Content Developer Center (includes publishing templates for Flash)

    Another approach which avoids JavaScript is the one known as Flash Satay, originally published in this article in A List Apart magazine. The author figured out how to make SWF files display in Netscape and other browsers which use the embed tag, but using only the object tag instead. It has more limitations than JavaScript approaches; but obviously, it doesn’t require JavaScript.

    See Also: Wikipedia: Flash Satay


  5. Paul S is reported to have said:

    I am trying to load a jpg from the url into a swf. I can get text to come in no problem, but for some reason cannot get the jpg to work. I tried passing the variable as text into this: loadMovie(“var”, “imageMC”); but have had no luck either. So how would I get this to work? Any help is greatly appreciated. And I’m using Flash 8.

    Thanks in advance.


  6. Mohammed Sunely is reported to have said:

    how can i Pass asp.net array to flash array?


  7. Paul is reported to have said:

    Hi Mohammed,

    The answer, as always, is “it depends.”

    If you only need to load the array data in once, the FlashVars approach described on this page should work just fine (see the section titled “Complex/Large amounts of data”)

    If you want to load an array several times, a different approach will be needed; you’ll probably want to look into using SOAP web services, Flash Remoting, or XML. I gave a presentation where I demonstrated/explained several of these approaches and their strengths and limitations; the files (and a link to the recorded presentation) are at this post: Slides and notes from “Flash to external data communication”.


  8. Paul is reported to have said:

    Paul S.:

    I’m not certain of exactly what you’re trying to do, but I’ll take a stab at it.

    In ActionScript 2 if you’re trying to load an external jpeg into a SWF file, you need to first create a MovieClip instance to contain it, then you need to load the jpeg into that MovieClip using loadMovie() (or even better, using the MovieClipLoader class).

    Here’s an example, using loadMovie():

    var container:MovieClip = this.createEmptyMovieClip("container_mc", this.getNextHighestDepth());
    
    container.loadMovie("img02747.jpg");

    Note that in this example I’m creating an empty movie clip to contain the loaded jpeg, but you could just as easily use a movie clip symbol placed on the stage — in that case you’d substitute the movie clip’s instance name for container and just use the second line of code: instanceName.loadMovie("url");

    Here’s another example, using a MovieClipLoader object, which allows you to determine when the image finishes loading, track loading progress, and other nice stuff like that:

    var container:MovieClip = this.createEmptyMovieClip("container_mc", this.getNextHighestDepth());
    
    var loader:MovieClipLoader = new MovieClipLoader();
    var listener:Object = new Object();
    listener.onLoadInit = function()
    {
        // stuff to do when the image finishes loading
        trace("image finished loading");
    };
    
    loader.addListener(listener);
    loader.loadClip("img02747.jpg", container);

  9. Chris is reported to have said:

    How do you pass a variable from flash back to the embed tags to the database?

    Cheers.


  10. Riaan is reported to have said:

    Thanks! This helped a lot!

    Very detailed, step by step explanations.
    The examples helped too.

    Arigato!
    Riaan Botha
    South Africa


  11. Markus is reported to have said:

    Is there a way to count the amount of variables passed in if the list will be dynamic - either using FlashVars or appended to a URL String?
    I wish to capture the amount of variables and place into an Array .. is there a default syntax in Flash that is like for example - FlashVars.length or flashVar[1] = “Me” where it would equal say “file.swf?myvar0=work&myvar1=Me”


  12. Kingsley's Blog is reported to have said:

    links from Technoratiarticle is the first in a series which describes how to connect a Flash project with a database, or more generally, how to pull external data into a .swf file playing in the Flash Player. The series introduction contains a list of all the articles. Flash and database communication Part 1: Using URL Variables and FlashVars, a bunch of Words, punctuated - by Paul Robertson (http://probertson.com/)


  13. Martin is reported to have said:

    You’ve put this together in a very straightforward and usable way.

    Thank you.


  14. Arran North is reported to have said:

    Hello there,

    I hope you are well,

    Thank you for the tutorial above.

    I have a slight problem though, not really in relation to the tut above but one of a different nature.

    Basically I am creating a fairly simple flash cart where of which the item number and price are shown.

    Now the way I have constructed it so far is very simple.
    Just given the movie vars etc but now I am having problems with the html coding;

    What I am trying to do is load an external movie and control the variables in that. Now how would I go about it?

    If you could reply via email that would be great, and if you would like your help in exchange for something please do not hesitate to ask.

    Many thanks,
    Arran


  15. Rusty is reported to have said:

    hello, excellent website…..
    If anyone can help me that would be great.

    So, I have a swf file that is being reused for my navigation buttons on some html pages.

    I would like to use FlashVars or something similar to control the swf when it is loaded so that when you click on one of the nav buttons when the page is reloaded it will automatically go to a certain movie clips timeline frame label. So in a sence it will go to the “selected” state for the clicked on button on reload. I hope that makes sence. If you can email me or post here that would be amazing. Any help is greatly appreciated.

    Rusty


  16. Chiranjeevi is reported to have said:

    Very very thank you for this simple tutorial
    Correctly I am in the need of this part in my present work and with out any time lag I found this and implimented in my project where I am playing a mediadisplay component.

    Thank you very much


  17. Flash Loadvars ASAP - Oman3D.com Forum is reported to have said:

    Kramer auto Pingback[…] What you are trying to accomplish is an advanced form of flash communication with another an external source of data, which I assume is a database. We do not have a tutorial on this topic, you can try learning more about Flash and database communication from this web page: http://probertson.com/articles/2005/…ars-flashvars/ Good luck! […]


  18. Peter is reported to have said:

    Is the FlashVars in a special format or something?
    I have a problem with FlashVars when I try to load them into a variable which later will be used to change a certain MovieClip.

    My FlashVars is like this:
    FlashVars=”roomNr=224”

    Inside Flash I declare a variable which is filled with the value from the FlashVar:
    varsRoomNr = roomNr;

    Then I have a movieclip named “224” which I want to make visible if its name is the same as the FlashVar.

    I try to use the variable varsRoomNr for this:
    varsRoomNr._visible = true;

    This won’t work… If I hard code the new variable to be 224 it works. But not if the variable is populated from FlashVars.

    So… is there a special string format on the FlashVars or something???

    Any suggestion would be appreciated.


  19. Paul is reported to have said:

    Hi Peter,

    I’m surprised that works at all, even with the variable hard-coded. If you pass in a value in FlashVars (e.g. “224”) it will use the value as a String variable, so the variable roomNr will automatically be treated as a String variable containing the value “224” (note, however, that since “224” can be converted to a number, the variable will properly be converted if it’s used as a number.

    When this line of code is run:
    varsRoomNr = roomNr;
    it copies the value in roomNr (“224”) to the variable varsRoomNr, so varsRoomNr also becomes a String variable containing the value “224”.

    Note that, even if you have a variable whose name matches the string value (including a movie clip whose instance name is the same as that value), the line varsRoomNr = roomNr won’t set varsRoomNr equal to that variable or movie clip — it will set it equal to the literal value in roomNr.

    There is a way to use a variable’s value as a movie clip name, using the square-bracket notation and referencing it as a child property of its parent (container) movie clip. For example, if the movie clip you want to show has the instance name “clip224” and its parent (container) movie clip has the instance name “allRooms” then you could use this code to change the clip’s visibility:

    // In FlashVars: FlashVars="roomNr=224"
    var clipToHide = "clip" + roomNr;
    allRooms[clipToHide]._visible = true;

    Likewise, if the movie clip you want to show is on the main timeline, you could use _root in place of allRooms (or, if the code is also on the main timeline, you could use this in place of allRooms).

    As a side note, although Flash may let you do it for an instance name, a variable name shouldn’t start with a digit, so “224” isn’t a valid variable name or instance name. As shown, you probably ought to give your movie clips instance names like “clip224” or “room224” or something like that. Then you can either pass the whole instance name in the FlashVars variable (e.g. flashvars="roomNr=224") or else concatenate the numerical value passed in to create the instance name (as shown in the code listing).


  20. Peter is reported to have said:

    Hi again. Thank you so much for your reply. Child clip did the trick!

    By the way, I DID have a letter first in the instance name. For example G224, so there was no problem.

    If any one is interested, here’s what I’ve done:

    I have a personal database of the employees at my department here at the university of Vaxjo, Sweden. I wanted to have a Flash map to show where they are located, so, based on a drawing, I’ve made a map over the building.
    I made a big MovieClip covering the whole building which I named “allRooms”.
    Inside that MovieClip I made one child MovieClip for each of the rooms in the building. I named these MovieClips the same as they are named in real life (which is also in the database of course).

    What I want now is to have the room highlighted for the person you are looking for.

    Using PHP I now populate the FlashVars on the webpage from the database so I get a FlashVars like this:
    FlashVars="roomNumber=G201E"

    I now script the frame like this:

    room = roomNumber;
    
    // code to change the color of the child movieclip, thus
    // highlighting the room in question
    import flash.geom.ColorTransform;
    var myCT:ColorTransform = new ColorTransform();
    
    // Pauls script idea of creating a child variable
    var clipToShow = room;
    
    myCT.rgb = 0x009900;
    // Pauls script at work.
    // The parent clip and the child clip to affect
    allRooms[clipToShow].transform.colorTransform = myCT;
    
    stop();
    

    Once again, Thank you Paul!!!

    Peter Häggstrand, University of Vaxjo, Sweden


  21. FlashVars som namn p movieclip (Flash) - MacWorld Forum - allt om Mac och iPod is reported to have said:

    Kramer auto Pingback[…] Inte mnga svar hr inte… men jag fick faktiskt ett svar p ntet, av en snubbe som jobbar p Adobe med utvecklingen av krnscripten fr just Flash. Oerhrt sympatisk kille…S hr skrev han, i fall ngon annan r intresserad av lsningen p sin sida […]


  22. Abdul is reported to have said:

    Anybody plz help me to send a email in flash using php.


  23. Techzine - Graphics & Webdesign - [FLASH] URL variablen in actionscript - List messages is reported to have said:

    Kramer auto Pingback[…] postsprivberichtDe variabelen die je daar opgeeft zijn gewoon beschikbaar in je flash bestand. http://probertson.com/art…abases-urlvars-flashvars/ //Edit: Schop naar G&W en topic titel bewerkt.joolee wijzigde dit bericht op 20-08-2007 20:25 […]


  24. Dave Welsh is reported to have said:

    Hi,

    I’m trying to create an array from text contained within an external file. In the external file are multiple variable names. I am passing the variable name to be turned into an array using a query string (based on the directory name). From my page source, I can see the correct string is being passed to the swf file.

    Previously, I had the variable name hardcoded in the external file ie:

    myAr1=this.myAd1.split(“|”); , myAr2=this.myAd2.split(“|”);

    ….etc. External variable names being myAd1, myAd2.

    I now want to have the external variable name taken from the URL query string ‘myAdvertiser’

    My html page source shows the passing of this variable name to be correct:

    and

    So far so good…

    The problem seems to arise when splitting the text into the array.

    myAr1=this.myAdvertiser.split(“|”);

    I think that the swf is attempting to find an actual variable in the text file called ‘myAdvertiser’ rather than using the information passed already from the query string ie myAdvertiser=flashtest2

    So my question is, how do I construct the array code to make use of the query string?

    Thanks in advance for any help you might be able to offer.

    Dave


  25. Dave Welsh is reported to have said:

    Sorry, please ignore my previous stuff - solved it but using:

    myAr1=this[myAdvertiser].split(“|”);

    AND by removing the javascript stuff from the Flash published html file.

    Thanks!


  26. perumal is reported to have said:

    one swf to another swf passing variables using actionscript


  27. Reg is reported to have said:

    I have playfile.swf file that works in an html page as show below:


    Please upgrade to the latest version of Flash Player.
    Click here if you already have Flash Player installed.

    //

    I would like to use playfile.swf in a new swf file by using loadMovie I’ve tried this

    this.createEmptyMovieClip(“test_mc”, 20);
    test_mc.loadMovie(“playfile.swf?id=8&name=test”);

    and this

    this.createEmptyMovieClip(“test_mc”, 20);
    test_mc.loadMovie(“playfile.swf”,”id=8,name=test”);

    with no joy, it works but uses the default values for id and name - as if called with no arguments

    The loadMovie has a signatures
    loadMovie( ” url “, level / target [ , variables ])
    and
    loadMovie( ” url “[ , variables ])

    What is [ , variables ] ? is it the arguments id and name in the snippet above?
    How can I use loadMovie where the swf takes arguments?

    Sorry for the long comment.


  28. Gatti is reported to have said:

    I’ve been trying the flashvars approach and can’t get it to work. This is truly driving me nuts. All I want is the html page to tell the flash file to gotoAndStop at a specific frame/label. (Inside the targeted frame label, it will execute code to set the flash file up)

    Here is the page example to look at:
    http://www.davisandco.com/testing/navigation-v11.html

    Please let me know what I’m doing wrong.


  29. kral oyun is reported to have said:

    I downloaded your example.
    I am looking for this ex. I think this is very good idea for FlashVars.
    Thanks your share.


  30. Rowan is reported to have said:

    I am going nuts here! I have followed your instructions step by step and my flash file displays nothing! I have tried both the URL variable sequence and the flashvars methods.

    I have a dynamic textbox on my stage with instance name myTextField_txt. On the same frame I have the code myTextField_txt.text = var1;

    In my html I have added ?var1=value after my swf filename in both the <param name tag and the <embed src tag.

    When I open the html file in my browser and add ?var1=value1 to the end of the url, my text field is blank?

    What am I missing?


  31. Paul is reported to have said:

    Hi Rowan,

    I’m honestly not sure exactly what the issue is. It sounds like you’re setting things up correctly. Without seeing the actual HTML and FLA files it’d be difficult to diagnose exactly what’s happening.

    One thing I can see (and perhaps I didn’t explain this very well in my article) is that for a plain HTML page, adding url parameters doesn’t make those parameters automatically get passed to the SWF file (in other words, the browser doesn’t do anything with those parameters). In order to have url parameters change the SWF parameters, that needs to be done on the web server (using something like PHP, ColdFusion, ASP.NET, etc.). So if you’re just viewing an HTML page from your hard drive and adding url parameters in the browser, it won’t do anything to change the content of the page (and consequently, the values won’t get passed to the SWF file).

    Hopefully that makes sense. Please feel free to ask if you have more questions.


  32. Paul is reported to have said:

    For the sake of others who might read this, I followed up with Rowan and we figured out what the issues are. The first one in particular is very relevant if you’re creating a FLA using Flash 8 or newer.

    Here’s a summary of the issues:

    It turns out there are two issues going on here.

    The first is that the published HTML file uses JavaScript to dynamically add the SWF to the HTML page (which is pretty much standard now, but wasn’t back when I wrote this article). So the url parameters you added to the and tags weren’t actually being used, because they’re in the tag (they’re the fallback in case a user has JavaScript turned off or has a browser that’s so old that it doesn’t support JavaScript).

    The good news is that it’s easy to make the JavaScript add additional values — all you have to do is add them to the big list of variable/value pairs in the AC_FL_RunContent() function call. All I had to do was add a comma to the last line in the function call and then add this line below it: ‘flashvars’, ‘var1=value’

    Note that this approach uses the “FlashVars” technique that I describe in the article, rather than the URL variables approach. There’s no easy way to use the url variables approach in this case.

    There was also another problem, in the FLA. The font type for that text field (the drop down list right below the font size and color) was set to “Anti-alias for animation”. First of all, that’s something that was added in Flash 8, so if you’re publishing a SWF for a Flash Player version older than 8 then that won’t be supported (unless the user has Flash Player 8 or above). In addition, in order to use “anti-alias for animation” you must embed the font you’re using in the SWF, by adding the font to the FLA’s library. To fix this issue, change the menu selection to “Use device fonts”.


  33. Greg is reported to have said:

    This is really handy. I hope to use code but i can only get it to work if all my text boxes are in the first frame of the SWF. I named my variables like this in the first frame:

    var var1:String;
    yourname.text = var1;

    var var2:String;
    boss.text = var2;

    var var3:String;
    grievances.text = var3;

    Is there a way to declare all the variables in the first frame and then have them appear later on in the timeline?

    Thanks!
    Greg


  34. Vikas is reported to have said:

    Hey,

    The sample files are really helpful. But I want to know how can you create placeholder for images inside fla, so that you can get images dynamically.

    Thanks!
    VP


  35. xjumper is reported to have said:

    dude.. i have spent the past week trying to figure this out. i google’d, asked friends who code in actionscript.. no one knew how to do this.. then i come to your site.. see your examples.. and bam it works.

    you rock. thank you!


  36. neeraj is reported to have said:

    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 page.

    Is it possible.


  37. Paul is reported to have said:

    @neeraj:

    Because FlashVars are only loaded one time, when the SWF file first loads, you can’t change the values at runtime.

    If you want to use JavaScript to make some change in a SWF, you can do so using the ExternalInterface class (also called the “External API”). The External API allows you to set up communication between ActionScript in a SWF and code running in the SWF’s container (JavaScript in an HTML page being the most common example).

    Here are some resources on the External API:
    - “Using the external API” (a chapter I wrote in “Programming ActionScript 3.0”)
    - “Example: Using the external API with a web page container” (a section of that chapter that deconstructs a specific example that uses JavaScript)
    - “ExternalInterface class” listing in the ActionScript 3.0 Language Reference


  38. Studious Maximum is reported to have said:

    Can you update this article to include a discussion of SWFObject and how it applies?


  39. Your page is now on StumbleUpon! is reported to have said:

    Kramer auto Pingback[…] Your page is on StumbleUpon […]


  40. kiz oyunlari is reported to have said:

    Hi.
    Can we load a external shockwave file (xxx.dcr or xxx.dir) with a flash loader file?
    Thanks for advise…


  41. Kylie is reported to have said:

    Can you take a variable from a query string passed to the movie, ie. http://www.mysite.com?id=4 and apply it to the text field in the movie rather than have the variable embedded into the html itself?


  42. Parameter an Flashanwendung bergeben - Flexforum.de is reported to have said:

    Kramer auto Pingback[…] Hallo, also Google zeigt schon einiges an Treffern. Bspl. http://probertson.com/articles/2005/…ars-flashvars/ Gru […]

Add your comment





Comment notes

Please keep comments on topic. Comments that are inappropriate or offensive will be edited or removed.

Paragraphs and line breaks are automatically converted to HTML, and quotation marks are converted to “smart” quotes.

The following XHTML tags can be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . All others will be removed.

Articles by Type

Articles by Topic

Random Reading

Currently...

Subscribe