title: How to tell if a <script> tag failed to load
body: <p>I'm dynamically adding <code>&lt;script&gt;</code> tags to a page's <code>&lt;head&gt;</code>, and I'd like to be able to tell whether the loading failed in some way -- a 404, a script error in the loaded script, whatever.</p>
  
  <p>In Firefox, this works:</p>
  
  <pre><code>var script_tag = document.createElement('script');
  script_tag.setAttribute('type', 'text/javascript');
  script_tag.setAttribute('src', 'http://fail.org/nonexistant.js');
  script_tag.onerror = function() { alert("Loading failed!"); }
  document.getElementsByTagName('head')[0].appendChild(script_tag);
  </code></pre>
  
  <p>However, this doesn't work in IE or Safari.</p>
  
  <p>Does anyone know of a way to make this work in browsers other than Firefox?</p>
  
  <p>(I don't think a solution that requires placing special code within the .js files is a good one. It's inelegant and inflexible.)</p>