Using FlashVars to load and display text

Using FlashVars is simple, but requires you to publish your SWF files along with HTML documents. You modify the generated HTML code and include the FlashVars properties in both the object and embed tags. You can then test the Flash document by viewing the modified HTML document in your web browser.

To use FlashVars to pass variables from HTML to your Flash document:

  1. Create a new Flash document and save it as flashvars.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    this.createTextField("my_txt", 10, 10, 10, 100, 21);
    my_txt.text = _level0.username;
    
  3. Save the Flash document and select File > Publish to generate the HTML and SWF files.

    NOTE

    An HTML document publishes, by default, to the same directory as your FLA file. If an HTML document does not publish, select File > Publish Settings and then select the Formats tab. Make sure that you select HTML.

  4. Open up the flashvars.html document in a text or HTML editor.
  5. In the HTML document, modify the code inside the object tag to match the following.

    The code you need to add is in boldface.

    <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="flashvars" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="flashvars.swf" />
    <param name="FlashVars" value="username=Thomas" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />
    <embed src="flashvars.swf" FlashVars="username=Thomas" quality="high" bgcolor="#ffffff" width="550" height="400" name="flashvars" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
    </object>
    
  6. Save your changes to the HTML document.
  7. Open the modified HTML in a web browser.

    The SWF file displays the name "Thomas" in the dynamically created text field on the Stage.

For information on security, see Understanding Security.