How Do I handle IOErrors From HTML Enabled Text Fieds in AS3
-
22-09-2019 - |
Question
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
OTHER TIPS
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
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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow