title: best way of dynamic script loading
body: <p>I need to load some js files dynamically and sequentially(i.e. second script loads after load complete of first one, third after second and so on).</p>
  
  <p>Question: how to detect when a script have been loaded? I have encountered problems with onload event - it not fires in IE8. After reading <a href="http://unixpapa.com/js/dyna.html" rel="noreferrer">this</a>, I tried to subscribe to onreadystatechange and wrote very ugly code for loading a script: </p>
  
  <pre><code>function loadScript(url, callback) {
          var isLoaded = false;
          var script = document.createElement('script');
  
          script.onreadystatechange = function () {
              if ((script.readyState == 'complete' || script.readyState == 'loaded') &amp;&amp; !isLoaded) {
                  if (callback) callback();
              }
          };
          script.setAttribute('type', 'text/javascript');
          script.setAttribute('src', url);
          document.head.appendChild(script);
      };
  </code></pre>
  
  <p>Can you suggest better cross browser solution without such tricks?</p>
  
  <p><strong>UPD:</strong>
  Thanks for answers. What should I do if I need also to load jquery.js(for example, client have old version) :)?</p>