title: Check if script is loaded already. If not, load it asynchronously
body: <p>I'm trying to load the jQuery JS asynchronously and then i want to call some callback methods when it is completely loaded. 
  I might be using the same code block over and over again. So i want to check if jQuery(or any other script) is already added before actually adding it.If it is already loaded/loading, then the callback of the current call should be appended to that of the previous call. This is what i'm trying to do.</p>
  
  <pre><code>/*BP Asynchronous JavaScript Loader*/
  if (typeof bp_onload_queue == 'undefined') var bp_onload_queue = [];
  
  if (typeof bp_dom_loaded == 'boolean') bp_dom_loaded = false;
  else var bp_dom_loaded = false;
  
  if (typeof bp_async_loader != 'function') {
      function bp_async_loader(src, callback, id) {
  
          var script = document.createElement('script');
          script.type = "text/javascript";
          script.async = true;
          script.src = src;
          script.id = id;
          //Check if script previously loaded.
          var previous_script = document.getElementById(id);
          if (previous_script) if (previous_script.readyState == "loaded" || previous_script.readyState == "complete") {
              callback();
              alert("had already loaded the same script completely");
              return;
          } else {
  
              script = previous_script;
          }
          if (script.onload != null) previous_callback = script.onload;
          script.onload = script.onreadystatechange = function() {
              var newcallback;
              if (previous_script &amp;&amp; previous_callback) newcallback = function() {
                  previous_callback();
                  callback();
              };
              else newcallback = callback;
              if (bp_dom_loaded) {
                  newcallback();
              } else bp_onload_queue.push(newcallback);
              // clean up for IE and Opera
              script.onload = null;
              script.onreadystatechange = null;
          };
          var head = document.getElementsByTagName('head')[0];
          if (!previous_script) head.appendChild(script);
          else alert("had already started loading that script but not complete.so we appended to the previous script's callback");
  
      }
  }
  
  if (typeof bp_domLoaded != 'function') function bp_domLoaded(callback) {
      bp_dom_loaded = true;
      var len = bp_onload_queue.length;
      for (var i = 0; i &lt; len; i++) {
          bp_onload_queue[i]();
      }
  } 
  /*JS gets loaded here */
  
  bp_domLoaded();
  
  /*Loading jQuery Asynchronously */
  bp_async_loader("http://code.jquery.com/jquery-1.4.4.js", function() {
      alert("script has been loaded : 1");
  }, "jQueryjs");
  
  bp_async_loader("http://code.jquery.com/jquery-1.4.4.js", function() {
      alert("script has been loaded : 2");
  }, "jQueryjs");
  </code></pre>
  
  <p>Can someone please let me know if there are any errors in this code or if there is a better way of doing this?</p>