Search code examples
apache-flexflashactionscript-3actionscriptflex3

Flash TextField HTML - How do I prevent the error dialogue for missing images? (Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found)


I'm using a Flash TextField control to display some HTML content inside a Flash presentation to be shown on a large touch-screen kiosk. Unfortunately, if any image tag in the displayed HTML content points to a non-existent image, a dialogue is shown with the error message

Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

I am trying to avoid having that dialogue pop up. The solution for loading content through a loader class is to catch IOErrorEvent.IO_ERROR, but I've tried listening for that on the TextField, on stage, Main and loaderInfo to no avail. I've tried wrapping the whole thing in a try-catch, and that also doesn't work.

Here's the simplified code I'm using to find solutions:

package {
    import flash.display.Sprite;
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    public class Main extends Sprite {

        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var html:TextField = new TextField();
            html.type = TextFieldType.DYNAMIC;
            html.multiline = true;
            html.htmlText = "Bogus image: <img src=\"foo.jpg\" />";         

            addChild(html);
        }
    }
}

Edit: And here's the entire working code.

For dynamic content and so forth, of course, you would need a list of images and a function to generate handlers, etc.

package {
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    public class Main extends Sprite {

        public function Main():void {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
                removeEventListener(Event.ADDED_TO_STAGE, init);

                var html:TextField = new TextField();
                html.type = TextFieldType.DYNAMIC;
                html.multiline = true;
                html.htmlText = "Bogus image: <img id=\"image\" src=\"foo.jpg\" />";                 

                var loader:Loader = html.getImageReference("image") as Loader;
                if(loader){         
                    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:Event):void {
                            trace("Error loading foo.jpg!");
                        });
                }               

                addChild(html);
        }
    }
}

Solution

  • You can add ID's to images

    html.htmlText = "Bogus image: <img src=\"foo.jpg\" id="image" />"; 
    

    And then setup IOErrorEvent handler to each image in HTML

    var loader:Loader = html.getImageReference("image") as Loader;
    if(loader){
        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:Event):void{});
    }