Tuesday, September 8, 2009

How to load your JavaScript in Website

1. Create two JavaScript files. The first contains just the code necessary to load JavaScript dynamically, the second contains everything else that’s necessary for the initial level of interactivity on the page.
2. Include the first JavaScript file with a script tag at the bottom of the page, just inside the .
3. Create a second script tag that calls the function to load the second JavaScript file and contains any additional initialization code.

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);
}

No comments: