How to dynamically load jQuery in a javascript function, call another function, and return that function's return value?
<p>I have a function which uses jQuery. I need to dynamically inject jQuery with JavaScript and have that function return that function’s return value. I would like to do something like</p>

<pre><code>(function(){
    var jQueryLoaded = false;
    var returnValue;
    if (!window.jQuery) {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'http://code.jquery.com/jquery-1.7.1.min.js';
        script.onreadystatechange= function () {
            if (this.readyState == 'loaded' || this.readyState == 'complete')
                onJQueryLoaded();
        };
        script.onload = onJQueryLoaded;
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    else {
        returnValue = myJQueryFunction();
        if (typeof(returnValue) == 'undefined')
            returnValue = true;
        return returnValue;
    }
    function myJQueryFunction() {
        // do something
    }
    function onJQueryLoaded() {
        if (jQueryLoaded)
            return;
        jQueryLoaded = true;
        var $ = jQuery;
        returnValue = myJQueryFunction();
        if (typeof(returnValue) == 'undefined')
            returnValue = true;
    }
    while (typeof(returnValue) == 'undefined'); // wait until onJQueryLoaded returns
    return returnValue;
})();
</code></pre>

<p>but <code>onJQueryLoaded</code> does not get called during the <code>while</code> loop.</p>

<p>Is there any way to have my base function wait until <code>onJQueryLoaded</code> gets called before returning?</p>
