May 29, 2010

SOLVED: Loader.close() causes error in Safari

When loading external assets in Actionscript 3.0, it is often necessary to close Loader requests before they have completed.

Consider the following scenario: A Flash image gallery that uses Loader objects to fetch images form the server.

  1. A user begins loading an image gallery
  2. For whatever reason the images are taking a long time to load (too many images, slow connection, etc.)
  3. The user grows tired of waiting and decides to click on something else, thereby loading new content.

Unless the gallery Loader objects are closed, they will continue to load in the background. Consequently the user will end up waiting even longer for new content to load since their bandwidth is being consumed by activity in the background.

Unfortunately removing the Loader objects from the stage is not enough to cancel them. To stop a Loader in progress we use the Loader.close() method like so:

try {
} catch(error:Error) {
// request was not open

This effectively closes the open connection but it presents a new problem. Safari will show an error message in the status bar for each cancelled request.

These errors are harmless, but let’s be honest, they look bad. One way to prevent the messages from showing is to set the window status property ahead of time to an empty string like so:

window.status=" "

This isn’t the prettiest solution but it works.