Select the following Topics you are interested
This "Javascript examplet" is extracted from article "Load a cross-server javascript dynamically - with memory leak fix in IE" from Cloudgen's Javascript Laboratory
To allowing loading cross domain Javascript code, JSON dynamically by a Javascript function with fixing the IE memory leak problem.
The following is the source code of the snippet. Click the link for downloading the snippet.
<script type="text/javascript">
function getScript(url,callBack){
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = url;
var done = false;
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState
|| this.readyState == "loaded"
|| this.readyState == "complete") ) {
done = true;
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
head.removeChild(script);
if(typeof callBack!="undefined") callBack();
}
}
head.appendChild(script);
return undefined;
}
</script>
The following is the application code of the snippet.
<script type="text/javascript">
if (typeof jQuery=="undefined")
getScript("http://jqueryjs.googlecode.com/files/jquery-1.3.2.js",function(){
$("div.result").text("jQuery Downloaded!")
});
</script>