Using FlashVars in an application

Using FlashVars to pass variables into Flash is similar to passing variables along the URL in the HTML code. With FlashVars, instead of passing variables after the filename, variables are passed in a separate param tag as well as in the embed tag.

To use FlashVars in a document:

  1. Create a new Flash document, and name it myflashvars.fla.
  2. Select File > Publish Settings and make sure that HTML is selected, and then click OK to close the dialog box.
  3. Add the following ActionScript to Frame 1 of the main Timeline:
    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;
    }
    

    NOTE

    By default, HTML code publishes to the same location as myflashvars.fla.

  4. Select File > Publish to publish the SWF and HTML files.
  5. Open the directory containing the published files (where you saved myflashvars.fla on your hard drive) and open the HTML document (myflashvars.html by default) in an HTML editor such as Dreamweaver or Notepad.
  6. Add the code that appears in bold below, so your HTML document matches the following:
    <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="myflashvars" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="myflashvars.swf" />
    <param name="FlashVars" value="myURL=http://weblogs.adobe.com/">
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />
    <embed src="myflashvars.swf" FlashVars="myURL=http://weblogs.adobe.com/" quality="high" bgcolor="#ffffff" width="550" height="400" name="myflashvars" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
    </object>
    

    This code passes a single variable called myURL, which contains the string
    http://weblogs.macromedia.com. When the SWF file loads, a property named myURL is created in the _level0 scope. One of the advantages of using FlashVars or passing variables along the URL is that the variables are immediately available in Flash when the SWF file loads. This means you don't have to write any functions to check if the variables have finished loading, which you would need to do if you loaded variables using LoadVars or XML.

  7. Save your changes to the HTML document, and then close it.
  8. Double click myflashvars.html to test the application.

    The text http://weblogs.macromedia.com, a variable in the HTML file, appears in the SWF file.

NOTE

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