Loading Variables From Query String Into Flash

Thursday, November 29th, 2007

Categories: Adobe Flash

RSS Comment Feed

Trackback

AddThis Social Bookmark Button

The title says it all; I will go over loading variables from your query string (located in the URL address bar) into Adobe Flash. This can serve many purposes. For example, you can set a language toggle via query string. This way, you can avoid a language splash screen and avoid having 4 different files to go through and update each file individually. Of course, there are many other uses, but that’s just one that I thought of.

Basics

  • A query string is located after the file name. I bolded it in the following example:
    www.pat-burt.com/index.php?variableName=variableValue
  • Use SWFObject. I can’t emphasize it enough, SWFObject gives you many freedoms and options. To top it off, it’s simple to use. Another reason to use SWFObject is that this example uses SWFObject.

In Your PHP/ASP File

On the page containing SWFObject, make sure you have a valid file extension for the language you’ll be using.

You’ll have a line that looks similar to this:

<script type=”text/javascript”>
var so = new SWFObject(“flashFile.swf”, “Title”, “550″, “400″, “8″, “#ffffff”);
so.write(“flash”);
</script>

Before the so.write(“flash”);, copy and paste this line:
ASP
so.addVariable(“varInFlash”, “<%=request.querystring(“variableName”)%>”);
PHP
so.addVariable(“varInFlash”, “<?=$_GET["variableName"]?>”);

Now what we’ve got is SWFObject passing the variable into Flash. Easy as pie.

Notes

  • We have not done any treatment to the query string we’ve pulled from the address bar, I strongly suggest treating the variable before you post it in addVariable to avoid errors.
  • SWFObject also uses the following line:
    so.addVariable(“varInFlash”, getQueryParamValue(“variableName”));
    Unfortunately, this doesn’t allow you to process the variable to avoid errors. You’ll have to take care of that in Flash. And if you’re Flash file is anything like mine, there’s a lot more room for ASP/PHP then there is for Actionscript

Using the Variable in Flash

As soon as you’ve done that, your variable is ready to use in Flash.

To check that the variable came in properly, create a Dynamic Text Box on your canvas and give it the instance name: textBox. On your first frame, type the following Actionscript:
textBox.text = varInFlash;
Run it, and voila, it works!

Hope that helped. Good luck. :)

Bookmark this blog using any bookmark manager!
Subscribe to Pat-Burt.com via RSS

Error. Page cannot be displayed. Please contact your service provider for more details. (1)



Related Posts


Subscribe to this Post

5 Responses to “Loading Variables From Query String Into Flash”

  1. stevo Says:

    10x a lot a great article

  2. Mike Higson Says:

    Hi there,

    I am trying to pass a variable from HTML (PHP) into Flash by uing:
    .swf?id=” />

    In Flash (ActionScript), I wish to use this variable to locate an XML file:
    treeDP_xml.load(“gallery_tree.xml”);

    I am guessing I need to do something like this but it is not working:
    treeDP_xml.load(_root.id”.xml”);

    Can anyone help me?
    Thanks.

  3. Patrick Burt Says:

    Which version of Flash/ActionScript are you using?

  4. Mike Higson Says:

    Flash CS3 Professional.

  5. Joe Says:

    Thanks for posting this article. It proved to be an invaluable resource for educating me how to pass variables to my flash using swfobject.