Inject jquery plus extra scripts into webpage through external js file
<p>I am trying to inject two js/jquery files into a webpage through an external js file. </p>

<p>For it to work i will need to inject a jQuery framework (it such does not already exists in the page) and afterwords injecting my own js files.</p>

<p>To make this work, I have been trying to generate a jquery injector but ended up using one by Karl Swedberg (http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet)</p>

<p>When you cut everything not needed (at least I think so) out of the file and adapt it a bit to my needs, i get:</p>

<pre><code>(function() {
var el=document.createElement('div'),
b=document.getElementsByTagName('body')[0];
otherlib=false;
if(typeof jQuery!='undefined') {
    //msg='This page already using jQuery v'+jQuery.fn.jquery;
}
else if (typeof $=='function') {
    otherlib=true;
}

// more or less stolen form jquery core and adapted by paul irish
function getScript(url,secondaryScripts, success){
    var script=document.createElement('script');
    script.src=url;
    //if (secondaryScripts)
//          script.appendChild(document.createTextNode('var jQuery = jQuery.noConflict();'));
    var head=document.getElementsByTagName('head')[0],
    done=false;
    // Attach handlers for all browsers
    script.onload=script.onreadystatechange = function(){
        if ( !done &amp;&amp; (!this.readyState
        || this.readyState == 'loaded'
        || this.readyState == 'complete') ) {
            done=true;
            success();
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
        }
    }
    ;
    head.appendChild(script);
}

getScript('http://code.jquery.com/jquery-latest.min.js',false, function() {
    if (typeof jQuery=='undefined') {
        //msg='Sorry, but jQuery wasn\'t able to load';
    }
    else {
        if (otherlib) {
            //msg+=' and noConflict(). Use $jq(), not $().';
            $jq=jQuery.noConflict()

        }
    }
});

getScript('jquery-ui-1.8.16.custom.min.js', true, function() {});
getScript('script-20111006-1516.js', true, function() {});      

}
)();
</code></pre>

<p>But it dosen't work and firebug report that jquery is not defined. Which seems correct since i can't execute any jquery even within this script that seems to work.</p>

<p>In my external files U wrap the: </p>

<pre><code>(function($, 
})(jQuery);
</code></pre>

<p>around since im trying to make my script not conflict with anything already running. But im probably also doing something wrong here as well :)</p>

<p>Hopes somebody can help me in the right direction.</p>

<p>Regards,
Dennis</p>
