views:

3397

answers:

3

Hi there, I've looked through the forums, and couldn't find the answer to my question outright, so I figured I'd just ask it. Apologies if this question has been asked already.

I'm trying to load an external html file into a dynamic text field. The dynamic text field has html display enabled. The html file contains a list of links in tags, and nothing more. The dynamic text field is called 'dynamictext', and I've tried:

var externaltextload:URLLoader = new URLLoader( );
externaltextload.load(new URLRequest("text/Links.html"));
externaltext.text=externaltextload.data;

and

var externaltextload:URLLoader = new URLLoader( );
externaltextload.load(new URLRequest("text/Links.html"));
externaltext.htmlText=externaltextload.data;

I'm probably way off with this. Any help out there?

Thanks in advance!

A: 

The URLLoader behaves asynchronously, meaning you can't expect an answer from the remote end immediately. You need to listen for an event signalling that the data has been downloaded.

Here's the example from the docs. I don't think you'll find a better example. Note how before the load operation, a load of listeners are registered with the URLLoader that will call the respective functions in the case of success or failure, in particular the line:

dispatcher.addEventListener(Event.COMPLETE, completeHandler);

which hooks the COMPLETE event which is signalled when the data is ready.

package {
 import flash.display.Sprite;
 import flash.events.*;
 import flash.net.*;

 public class URLLoaderExample extends Sprite {
  public function URLLoaderExample() {
   var loader:URLLoader = new URLLoader();
   configureListeners(loader);

   var request:URLRequest = new URLRequest("urlLoaderExample.txt");
   try {
    loader.load(request);
   } catch (error:Error) {
    trace("Unable to load requested document.");
   }
  }

  private function configureListeners(dispatcher:IEventDispatcher):void {
   dispatcher.addEventListener(Event.COMPLETE, completeHandler);
   dispatcher.addEventListener(Event.OPEN, openHandler);
   dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
   dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  }

  private function completeHandler(event:Event):void {
   var loader:URLLoader = URLLoader(event.target);
   trace("completeHandler: " + loader.data);

   var vars:URLVariables = new URLVariables(loader.data);
   trace("The answer is " + vars.answer);
  }

  private function openHandler(event:Event):void {
   trace("openHandler: " + event);
  }

  private function progressHandler(event:ProgressEvent):void {
   trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
  }

  private function securityErrorHandler(event:SecurityErrorEvent):void {
   trace("securityErrorHandler: " + event);
  }

  private function httpStatusHandler(event:HTTPStatusEvent):void {
   trace("httpStatusHandler: " + event);
  }

  private function ioErrorHandler(event:IOErrorEvent):void {
   trace("ioErrorHandler: " + event);
  }
 }
}
spender
Ahh, you are right. I need to wrap that last line in a function that is triggered by an event listener for the completed load. I've done that, and now the text of the html file appears, but not the links themselves. It appears that flash is treating the html contents as a string, and not as html. Any thoughts?
A: 

You can't really load an HTML page into a text-field. What you need to do is load an XML file that has HTML node and then display that string into the text-field, as that will render.

Sample XML (myfile.xml):

 <?xml version="1.0"?>
        <myhtml>
        <![CDATA[
        <a href="http://www.google.com"&gt;google&lt;/a&gt;&lt;br />
        <a href="http://www.yahoo.com"&gt;yahoo&lt;/a&gt;&lt;br />
        <a href="http://www.bing.com"&gt;bing&lt;/a&gt;&lt;br />
        ]]>
        </myhtml>

Sample AS3:

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("myfile.xml"));
xmlLoader.addEventListener(Event.COMPLETE, displayHTML);

function displayHTML (e:Event):void
{
    var xmlData = new XML(e.target.data);
    //this is your text-field
    mytextfield.htmlText = xmlData.myhtml;
}

This should work, get back to me if you need more help, I wrote this on the fly, so there might be a typo here or there.

joseeight
A: 

hmm, i tried Jose's method but doesnt seem to work!

with an error it says: "The class or interface "Event" could not be loaded. The class or interface "URLLoader" could not be loaded.

function displayHTML (e:Event):void { var xmlData = new XML(e.target.data);

These 2 sentences of code has some problems.

Sorry I'm a noob and your advise would be good!

winnie