title: Flash TextField HTML - How do I prevent the error dialogue for missing images? (Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found)
body: <p>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 </p>
  
  <pre><code>Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
  </code></pre>
  
  <p>I am trying to avoid having that dialogue pop up. The <a href="http://www.kirupa.com/forum/showthread.php?t=287501" rel="nofollow noreferrer">solution</a> for loading content through a loader class is to catch <code>IOErrorEvent.IO_ERROR</code>, 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.</p>
  
  <p>Here's the simplified code I'm using to find solutions:</p>
  
  <pre><code>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: &lt;img src=\"foo.jpg\" /&gt;";         
  
              addChild(html);
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit: And here's the entire working code.</strong></p>
  
  <p>For dynamic content and so forth, of course, you would need a list of images and a function to generate handlers, etc.</p>
  
  <pre><code>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: &lt;img id=\"image\" src=\"foo.jpg\" /&gt;";                 
  
                  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);
          }
      }
  }
  </code></pre>