Using LoadVars to load and display text

You can also use the LoadVars class to load content into a SWF file, which loads text or variables from an external file on the same server, or even content from a different server. The next example demonstrates how to dynamically create a text field and populate it with the contents of a remote text file.

To use LoadVars to populate a text field with external text:

  1. Create a new Flash document and save it as loadvarsText.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    this.createTextField("my_txt", 10, 10, 10, 320, 100);
    my_txt.autoSize = "left";
    my_txt.border = true;
    my_txt.multiline = true;
    my_txt.wordWrap = true;
    
    var lorem_lv:LoadVars = new LoadVars();
    lorem_lv.onData = function (src:String):Void {
        if (src != undefined) {
            my_txt.text = src;
        } else {
            my_txt.text = "Unable to load external file.";
        }
    }
    lorem_lv.load("http://www.helpexamples.com/flash/lorem.txt");
    

    The first block of code in the previous snippet creates a new text field on the Stage and enables multiline and word wrapping. The second block of code defines a new LoadVars object that is used to load a text file (lorem.txt) from a remote web server and display its contents into the my_txt text field created earlier.

  3. Save the Flash document and select Control > Test Movie to test the SWF file.

    After a slight delay, Flash displays the contents of the remote file in the text field on the Stage.

For information on security, see Understanding Security.


Flash CS3


 

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

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