What the hell? Why is everyone here advocating document.write ()? Sure enough that we moved on to this as standard practice at this point; document.write is not even valid if you are in XHTML customization.
The best way to do this would be as follows (also here, for better highlighting / parsing: https://gist.github.com/767131 ):
var loadScript = function(src, callbackfn) {
var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.setAttribute("async", "true");
newScript.setAttribute("src", src);
if(newScript.readyState) {
newScript.onreadystatechange = function() {
if(/loaded|complete/.test(newScript.readyState)) callbackfn();
}
} else {
newScript.addEventListener("load", callbackfn, false);
}
document.documentElement.firstChild.appendChild(newScript);
}
if(a) {
loadScript("lulz.js", function() { ... });
} else {
loadScript("other_lulz.js", function() { ... });
}
jQuery , loadScript (ala $.getScript ..).