The best way to load external JavaScript

leggi articolo

function loadScript(url, callback){

var script = document.createElement("script")
script.type = "text/javascript";

if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function(){
callback();
};
}

script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}

posted @ martedì 18 agosto 2009 12:56

Print

Comments on this entry:

# re: The best way to load external JavaScript

Left by LudovicoVan at 18/08/2009 15:11
Gravatar
> When <script> tags are in the flow of an HTML document, the browser must stop rendering and wait for the script file to download and execute before continuing (example). Creating a new <script> tag via JavaScript avoids this issue because it’s out of the flow of the document

Non conosce JavaScript (pochi lo conoscono): gli script andrebbero "semplicemente" inclusi in fondo alla pagina, e il flusso di esecuzione impostato di conseguenza.

-LV
Comments have been closed on this topic.
«ottobre»
domlunmarmergiovensab
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789