views:

521

answers:

2

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?

A: 

you have to use a try catch block:

try
{
  var textField:TextField = new TextField();
  textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
}
catch( error:IOError )
{
    //handle IOError
}
Jochen Hilgers
This doesn't actually work, thus the question (and the question it duplicates)
Jesse Millikan
@Jochen Hilgers No this can't work, use getImageReference.
Patrick
+3  A: 

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

Patrick