View comments | RSS feed

Using variables from the URL

When you develop an application or simple example in Flash, you might want to pass values from an HTML page into your Flash document. The passed values are sometimes known as the query string, or URL-encoded variables. URL variables are useful if you want to create a menu in Flash, for example. You can initialize the menu to show the correct navigation by default. Or you can build an image viewer in Flash and define a default image to show on the website.

To use URL variables in a document:

  1. Create a Flash document, and name it urlvariables.fla.
  2. Select File > Save As, and save the document on your desktop.
  3. Select Frame 1 of the Timeline, and add the following code in the Actions panel:
    this.createTextField("myTxt", 100, 0, 0, 100, 20);
    myTxt.autoSize = "left";
    myTxt.text = _level0.myURL;
    
  4. Select Control > Test Movie to test the SWF file in Flash Player.

    The text field displays undefined. If you want to make sure the variables are properly defined before you proceed, you need to check for the existence of the variables in Flash. You can do this by checking to see if they are undefined.

  5. To check to see if the variable is defined, modify the ActionScript you added to the Actions panel in step 3 to match the following code. Add the code that appears in bold:
    this.createTextField("myTxt", 100, 0, 0, 100, 20);
    myTxt.autoSize = "left";
    if (_level0.myURL == undefined) {
        myTxt.text = "myURL is not defined";
    } else {
        myTxt.text = _level0.myURL;
    }
    

    When you publish your Flash document, an HTML document is created by default in the same directory as the SWF file. If an HTML file was not created, select File > Publish settings, and make sure you select HTML in the Formats tab. Then publish your document again.

    The following code demonstrates the HTML in the document that is responsible for embedding a Flash document in an HTML page. You need to look at this HTML to understand how URL variables work in the following step (where you add additional code for URL variables).

    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,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.adobe.com/go/getflashplayer" />
    </object>
    
  6. To pass variables from the generated HTML document to your Flash document, you can pass variables after the path and filename (urlvariables.swf). Add the bold text to the HTML file that was generated on your desktop.
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="urlvariables" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="urlvariables.swf?myURL=http://weblogs.macromedia.com" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />
    <embed src="urlvariables.swf?myURL=http://weblogs.macromedia.com" quality="high" bgcolor="#ffffff" width="550" height="400" name="urlvariables" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
    </object>
    
  7. If you want to pass multiple variables to Flash, you need to separate the name/values pairs with an ampersand (&). Find the following code from step 6:
    ?myURL=http://weblogs.macromedia.com
    

    Replace it with the following text:

    ?myURL=http://weblogs.macromedia.com&myTitle=adobe+News+Aggregator
    

    Remember, you need to make the same changes to both the object tag and the embed tag to maintain consistency between all browsers. You might notice that the words are separated by + punctuators. The words are separated this way because the values are URL-encoded and the + punctuator represents a single blank space.

    NOTE

     

    For a list of common URL-encoded special characters, see the Flash TechNote, URL Encoding: Reading special characters from a text file.

    Because the ampersand (&) serves as a delimiter for different name/value pairs, if the values you are passing contain ampersands, unexpected results might occur. Given the nature of name/value pairs and parsing, if you had the following values being passed to Flash:

    my.swf?name=PB+&+J&flavor=strawberry+rhubarb
    

    Flash would build the following variables (and values) into the root scope:

    'name': 'PB ' (note space at end of value)
    ' J': '' (note space at beginning of variable name and an empty value)
    'flavor': 'strawberry rhubarb'
    

    To avoid this, you need to escape the ampersand (&) character in the name/value pair with its URL-encoded equivalent (%26).

  8. Open the urlvariables.html document, and find the following code:
    ?myURL=http://weblogs.macromedia.com&myTitle=Adobe+News+Aggregator
    

    Replace it with the following code:

    ?myURL=PB+%26+J&flavor=strawberry+rhubarb
    
  9. Save the revised HTML, and test your Flash document again.

    You see that Flash created the following name/value pairs.

    'name': 'PB & J'
    'flavor': 'strawberry rhubarb'
    

    NOTE

     

    All browsers will support string sizes as large as 64K (65535 bytes) in length. FlashVars must be assigned in both the object and embed tags in order to work on all browsers.


Flash CS3


Comments


ntrvrs said on Aug 15, 2007 at 8:27 PM :
I just tried this help tutorial exactly using CS3 software and it did not work
YourHideousness said on Sep 5, 2007 at 2:38 PM :
there's no connection of "myURL" to an action scrip property
SeventhSun DK said on Sep 7, 2007 at 2:33 AM :
I tried the same thing.

It doesn't seem to work.
Tian99ty said on Sep 30, 2007 at 4:20 AM :
Its for AS2, even though its CS3, you can compile with AS2 setting.

I need to find out the syntax for CS3 though...
adbe_paul said on Oct 2, 2007 at 3:37 PM :
Tian99ty is correct -- this page of the documentation refers to ActionScript 2.0, which you can use in Flash CS3 (even with Player 9) by changing the ActionScript version in the publish settings.

To access FlashVars in ActionScript 3.0 (in Flash) you use the LoaderInfo.parameters property for the LoaderInfo instance associated with your SWF's main timeline. For a description and working code sample, see this blog post by Peter deHaan:
http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html

(For Flex you use a different technique, which is discussed in the comments at the bottom of Peter's post.)
No screen name said on Nov 14, 2007 at 11:40 AM :
It did not work for me either. Then I got rid of all the RunActiveContent scripts (and the noscript tag) and now it's working :-) I guess the javascript routine writes its own object + embed tags and doesn't know about the querystring values you put inside the noscript tag... there should be a way to pass querystrings to the javascript as a param (maybe there is?) Then you'll only have to set this in 3 places !!
adbe_paul said on Nov 15, 2007 at 2:01 PM :
@ No screen name:

If you want to use FlashVars in conjunction with the RunActiveContent scripts, you need to add an additional name/value pair into the code that calls the AC_FL_RunContent() function.

If you look at the source you'll see it calls AC_FL_RunContent() and inside the parentheses there's a big list of pairs of values. Just add 'flashvars', 'var1=value&var2=value2', into that list (it doesn't matter where) and it will add the url variables into the script-generated <object> and <embed> tags.
No screen name said on Dec 1, 2007 at 1:45 AM :
@ adbe_paul:
Thank you! that was very helpful... saved my life ;)
Willy jojo said on Dec 7, 2007 at 1:22 PM :
Yeah I couldn't get this working. Taking away the <noscript> tags will show
the flash file twice and you'll see it works with <embed> but not the
<params>

And I did make sure I used AS2 with player 9.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000667.html