title: JavaScript "include" function?
body: <p>I was wondering if there is a javascript "include" function (similar to the one in python), and I was looking for something but couldn't find anything except <code>$.load()</code> and <code>google.load()</code>.</p>
  
  <p>So, I ventured out to create a script which could do that just for fun, so:</p>
  
  <pre><code>var include = function( lib ) {
  // start with jQuery
      if(lib == "jquery") {
          var script = document.createElement("script");
          script.type = "text/javascript";
          script.src = "http://code.jquery.com/jquery.min.js";
          script.defer = "defer"; // should I use script.setAttribute('defer', 'defer')?
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(script, s);
      }
  }
  </code></pre>
  
  <p>And then, in a separate script:</p>
  
  <pre><code>include("jquery");
  $("p").innerHTML = "Worked!";
  </code></pre>
  
  <p>But I got an error <kbd>$ is not defined</kbd></p>
  
  <p>Which makes sense, because the script is running before jQuery is loaded. So my question is, is there a way to make sure the include script runs <em>before</em> anything else ahead of it? I've seen <code>callback</code> solutions that look something like this:</p>
  
  <pre><code>include("http://code.jquery.com/jquery.min.js", function() {
      $("p").innerHTML = "Worked!";
  });
  </code></pre>
  
  <p>But I do wonder if there is anything (like I proposed above) that's a little more neat.</p>
  
  <p>Any help would be much appreciated!</p>