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?

jochil
  • 1,074
  • 6
  • 12
Howard Zoopaloopa
  • 3,798
  • 14
  • 48
  • 87
  • duplicate:http://stackoverflow.com/questions/309713/flash-textfield-html-how-do-i-prevent-the-error-dialogue-for-missing-images-e I didn't actually test the solution I accepted (no time at the time), so let me know if it does/doesn't work for you. (Edit: Okay, I'm not that lazy, I'm trying it out...) – Jesse Millikan Feb 22 '10 at 09:02
  • looks like you promised to test it in may last year! – Howard Zoopaloopa Feb 22 '10 at 16:35

3 Answers3

4

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

George Profenza
  • 50,687
  • 19
  • 144
  • 218
Patrick
  • 15,702
  • 1
  • 39
  • 39
2

i've solution for ya:

tField.addEventListener( Event.ADDED, addedObjectToFieldHandler, true );

function addedObjectToFieldHandler( event:Event ):void
{
   if ( event.target is Loader )
   {
       ( event.target as Loader ).contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function( e:IOErrorEvent ):void{} );
   }
}

that will prevent flashplayer from throwing errors and crash whenever image link is broken

sra
  • 23,820
  • 7
  • 55
  • 89
-1

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
}
jochil
  • 1,074
  • 6
  • 12