动态加载及判断js文件

动态加载JS文件

 

	
	function include_js(file) {        
		var _doc = document.getElementsByTagName('head')[0];        
		var js = document.createElement('script');        
		js.setAttribute('type', 'text/javascript');         
		js.setAttribute('src', file);         
		_doc.appendChild(js);        
		if (document.all) { //如果是IE              
			js.onreadystatechange = function () {   
				alert("state="+js.readyState);
				if (js.readyState == 'loaded' || js.readyState == 'complete') {                         
					alert('IE6、IE7 support js.onreadystatechange'); 
					alert($.fn) ;
				}             
			}        
		} else {               
			js.onload = function () {                          
				alert('Firefox、chrome and others support js.onload');               
			}         
		}  
	} 


判断js是否已经加载

 

var scripts = document.getElementsByTagName("script");
		for(i=0;i<scripts.length;i++){
			var currentScript = scripts[i];
			if (currentScript.readyState == 'loaded' || currentScript.readyState == 'complete'){
				alert(currentScript.src);
			}
		}


 

另外参考:http://blog.csdn.net/code_cj/article/details/6404418

你可能感兴趣的:(动态加载及判断js文件)