So che la mia risposta è un po 'in ritardo per questa domanda, ma, ecco un grande articolo su www.html5rocks.com - Immergiti nelle acque torbide del caricamento degli script .
In quell'articolo si è concluso che per quanto riguarda il supporto del browser, il modo migliore per caricare dinamicamente il file JavaScript senza bloccare il rendering del contenuto è il seguente:
Considerando che hai quattro script nominati script1.js, script2.js, script3.js, script4.js, puoi farlo applicando async = false :
[
'script1.js',
'script2.js',
'script3.js',
'script4.js'
].forEach(function(src) {
var script = document.createElement('script');
script.src = src;
script.async = false;
document.head.appendChild(script);
});
Ora, Spec dice : Scarica insieme, esegui in ordine non appena tutto il download.
Firefox <3.6, Opera dice: Non ho idea di cosa sia questa cosa "asincrona", ma succede che eseguo gli script aggiunti tramite JS nell'ordine in cui sono stati aggiunti.
Safari 5.0 dice: Capisco "asincrono", ma non capisco impostandolo su "falso" con JS. Eseguirò i tuoi script non appena atterreranno, in qualunque ordine.
IE <10 dice: Nessuna idea di "asincrono", ma c'è una soluzione alternativa usando "onreadystatechange".
Tutto il resto dice: sono tuo amico, lo faremo dal libro.
Ora, il codice completo con IE <10 soluzione alternativa:
var scripts = [
'script1.js',
'script2.js',
'script3.js',
'script4.js'
];
var src;
var script;
var pendingScripts = [];
var firstScript = document.scripts[0];
// Watch scripts load in IE
function stateChange() {
// Execute as many scripts in order as we can
var pendingScript;
while (pendingScripts[0] && pendingScripts[0].readyState == 'loaded') {
pendingScript = pendingScripts.shift();
// avoid future loading events from this script (eg, if src changes)
pendingScript.onreadystatechange = null;
// can't just appendChild, old IE bug if element isn't closed
firstScript.parentNode.insertBefore(pendingScript, firstScript);
}
}
// loop through our script urls
while (src = scripts.shift()) {
if ('async' in firstScript) { // modern browsers
script = document.createElement('script');
script.async = false;
script.src = src;
document.head.appendChild(script);
}
else if (firstScript.readyState) { // IE<10
// create a script and add it to our todo pile
script = document.createElement('script');
pendingScripts.push(script);
// listen for state changes
script.onreadystatechange = stateChange;
// must set src AFTER adding onreadystatechange listener
// else we’ll miss the loaded event for cached scripts
script.src = src;
}
else { // fall back to defer
document.write('<script src="' + src + '" defer></'+'script>');
}
}
Qualche trucco e minificazione in seguito, sono 362 byte
!function(e,t,r){function n(){for(;d[0]&&"loaded"==d[0][f];)c=d.shift(),c[o]=!i.parentNode.insertBefore(c,i)}for(var s,a,c,d=[],i=e.scripts[0],o="onreadystatechange",f="readyState";s=r.shift();)a=e.createElement(t),"async"in i?(a.async=!1,e.head.appendChild(a)):i[f]?(d.push(a),a[o]=n):e.write("<"+t+' src="'+s+'" defer></'+t+">"),a.src=s}(document,"script",[
"//other-domain.com/1.js",
"2.js"
])