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.
«novembre»
domlunmarmergiovensab
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567