Previous Page
Next Page

Recipe 19.3. Loading a Block of Text (Including HTML and XML)

Problem

You want to load a block of text, possibly some HTML or XML.

Solution

Use the URLLoader.load( ) method in conjunction with DataFormat.TEXT as the dataFormat to read the text from a file or output from a server-side script.

Discussion

ActionScript 3.0 handles loading text differently than ActionScript 1.0 and 2.0. In previous versions, there were two different callback methods that you could define on a LoadVars instance to interpret data loaded from a URL. The onLoad( ) callback method was invoked when URL-encoded data was processed. The onData( ) method fired when the data was finished downloading, but before it was processed (before onLoad( )). By using onData( ) instead of onLoad( ), you were able to access the text that was downloaded before it was processed as URL-encoded data and lost.

The flash.net.URLLoader class behaves differently than LoadVars, its predecessor. The URLLoader does not distinguish between data that has finished downloading and data that has been processed and decoded. Rather, there is only a complete event broadcast when data finishes download, and the dataFormat property determines how the downloaded data is interpreted. Setting the dataFormat property to DataFormat.TEXT instructs the URLLoader instance to interpret the data as plain text. In Recipes 19.1 and 19.2, the dataFormat property is set to DataFormat.VARIABLES to interpret the data as URL-encoded variables. By default, a URLLoader instance processes the data downloaded as text.

Consider that you want to load the following HTML text from an external file called example.html, and then display it in an HTML-enabled text field:

<b>Title:</b> ActionScript 3.0 Cookbook<br />
<b>Authors:</b> Joey Lott, Darron Schall, Keith Peters<br />
<b>Publisher URL:</b> <a href="http://www.oreilly.com">www.oreilly.com</a>

The following ActionScript class works to load text from the file, and then displays the text in an HTML-enabled text field:

package {
  import flash.display.*;
  import flash.text.*;
  import flash.events.*
  import flash.net.*;
  
  public class HTMLLoadingExample extends Sprite {
    private var _output:TextField;
    
    public function HTMLLoadingExample(  ) {
      initializeOutput(  );
      
      loadData(  );
    }
    
    private function initializeOutput(  ):void {
      _output = new TextField( );
      _output.width = stage.stageWidth;
      _output.height = stage.stageHeight;
      _output.html = true; // Enable HTML for the text field
      
      addChild( _output );    
    }
    
    private function loadData(  ):void {
      var loader:URLLoader = new URLLoader(  );
      
      // Instruct the loader to read the file as plain text - This line is not
      // necessary because the dataFormat is DataFormat.TEXT by default.
      loader.dataFormat = DataFormat.TEXT;
      
      // Register an event handler for when the data is finished downloading
      loader.addEventListener( Event.COMPLETE, handleComplete );
      
      // Load the HTML text from the example.html file
      loader.load( new URLRequest( "example.html" ) );
    }
    
    private function handleComplete( event:Event ):void {
      var loader:URLLoader = URLLoader( event.target );
      
      // Assign the htmlText of the text field to the HTML text that was contained
      // in example.html. The data property of the URLLoader is the file contents.
      _output.htmlText = loader.data;
    }
  }
}

The data property takes on different characteristics based on what dataFormat is set to. When the dataFormat is DataFormat.TEXT, the data property of the URLLoader instance is a String containing the text that was inside of the file that was loaded. When the dataFormat is set to DataFormat.VARIABLES, the data property is an Object that maps variable names with their values. When the dataFormat is set to DataFormat.BINARY, the data property becomes a flash.util.ByteArray instance.

See Also

Recipes 19.1 and 19.2


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)