Examplet Center

Javascript Examplets and Examples

Select the following Topics you are interested

  1. List of all Javascript Examplets and Examples

Javascript Examplet - Load a cross-server javascript dynamically - with memory leak fix in IE

This "Javascript examplet" is extracted from article "Load a cross-server javascript dynamically - with memory leak fix in IE" from Cloudgen's Javascript Laboratory

 


Statement of Goal

To allowing loading cross domain Javascript code, JSON dynamically by a Javascript function with fixing the IE memory leak problem.

 


Snippet:

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>

Application Note:

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>

Output:


Copyright 2010 Cloudgen Wong