Loading And Executing jQuery In Javascript File
<p>My question is similar to <a href="http://stackoverflow.com/questions/10113366/load-jquery-with-javascript-and-use-jquery">this one</a>. But I feel the answers there are incomplete and the question ended with a not of uncertainty...</p>

<p>I am developing a Javascript application for my users to put on their site. I only want them to have to put ONE javascript include at the top of their webpage in order to use my application.</p>

<p>My javascript needs jQuery to run.</p>

<p>So the goal is to...</p>

<ol>
<li>Check if jQuery is already loaded.</li>
<li>Load jQuery if it has not been loaded.</li>
<li>Execute javascript that needs jQuery to run.</li>
</ol>

<p>I want to do the above three all within the same script.</p>

<p>Here is what I tried originally...</p>

<pre><code>function loa_j(){
 if (typeof jQuery === "undefined") {
 var newscript = document.createElement('script');
 newscript.type = 'text/javascript';
 newscript.async = true;
 newscript.src = 'http://code.jquery.com/jquery-latest.min.js';
 (document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript);
 }
}

loa_j();
$(document).ready(function(){
//...Code That Never Fired
}
</code></pre>

<p>This does <strong>not</strong> work because javascript does not wait for jQuery to load. It does load the jQuery, just not fast enough.</p>

<p>Here is my other solution, this one worked, but I am scratching my head wondering whether or not this is really the best way to do this...</p>

<pre><code>function loa_j(){
if (typeof jQuery === "undefined") {
(function(a,b){function G(a){var b=F[a]={.... //Entire jQuery copy and pasted in :D
}
}
</code></pre>

<p>So by copying and pasting the entire jQuery into that function, the code actually does work.</p>

<p>Is this really the best way to do this?</p>
