Search code examples
actionscript-3error-handlingtextfieldhtmltextioerror

How Do I handle IOErrors From HTML Enabled Text Fieds in AS3


If I'm loading images via the <img> tag in a dynamic text field and an IOError is thrown, what would I attach the event listener too? the text field? I tried this...

var textField:TextField = new TextField();
textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
textField.addEventListener(IOErrorEvent.IOError, function (e:Event):void { trace("error caught") });

to no avail...

Suggestions?


Solution

  • You have to set an id to img and then use it within getImageReference on your TextField to get the Loader where you can add all the Event you want:

    import flash.display.Loader;
    import flash.events.IOErrorEvent;
    import flash.text.TextField;
    
    //...
    var tfd:TextField = new TextField();
    tfd.htmlText = 
          "here is some text <img id='myImg' src='image.jpg' /> and then some more";
    var ldr:Loader = tfd.getImageReference("myImg") as Loader;
    if (ldr != null) {
     ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    }
    //...
    private function onIOError(e:IOErrorEvent):void{
     //...
    }
    

    Another example here if you want