JavaScript – Test if a library failed to load

A small simple JavaScript block to detect and alert you if an external library fails to load:

try {
   switch( "undefined" ) {
      case typeof $: throw "jQuery";
      case typeof $.ui: throw "jQuery UI";
      case typeof TinyMCE: throw "tinyMCE";
      ...
      case typeof yourFavouriteLibrary: throw "Your favourite library";
   }
} catch( missingLibrary ) {
   alert("Error: failed to load " + missingLibrary);
}
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>