日期:2014-05-16  浏览次数:20764 次

执行innerHTML时候,代码片段的js
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
	<title>大屏幕</title>
	<script type="text/javascript" src="jquery.js" ></script>
  	<style type="text/css">
  	</style>
</head>
<body>
	<div id="cnt"></div>
	<input id="btn"  value="加载" type="button"/>
	
	<script>
		var excScript = (function(){
			var timer = 0,
				repeat = 0,
				head = document.getElementsByTagName("head")[0],
				queue = [];
			function createTmpScript(scriptEl,idx){
				var xmlhttp = new XMLHttpRequest();
				xmlhttp.onreadystatechange = function(){
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
						console.log("idx:" + idx)
						queue[idx].text = xmlhttp.responseText;
						queue[idx].load = true;
					}
				};
				xmlhttp.open("GET",scriptEl.src,true);
				xmlhttp.send(null);
			}
			
			function doExc(item){
				console.log("doExc")
				var script = document.createElement('script');  
				script.type = 'text/javascript';  
				var code = item.text;  
				//console.log("code:" + code)
				try{  
				    script.appendChild(document.createTextNode(code));  
				}catch(ex){  
				    script.text = code;//this works in all browers.  
				}  
				//console.log(code)
				item.el.parentNode.replaceChild(script,item.el);  
			}
			
			function excQueue(){
				console.log('queue.length:' + queue.length);
				if(queue.length){
					console.log('queue[0]')
					if(queue[0].load){
						doExc(queue.shift());
						arguments.callee();
					}else{
						repeat++;
						if(repeat < 10){
							console.log("repeat:" + repeat);
							setTimeout(arguments.callee,300);	
						}
						
					}
				}
			}
			
			function initQueue(scripts){
				var script = null,
					i = 0, len = scripts.length;
				for(i=0; i < len; i++){
					script = scripts[i];
					if(!script.type || script.type == "text/javascript"){
						if(script.src){
							createTmpScript(script,i);
							queue.push({
								"el": script,
								"text" : null,
								"load": false
							});
						}else{
							queue.push({
								"el": script,
								"load": true,
								"text" : script.text || script.textContent || script.innerHTML
							});
						}
					}
				}
			}
			return function(scripts){
				initQueue(scripts);	
				console.log(queue)
				excQueue();			
			};
		})();
		jQuery("#btn").click(function(){
			
			jQuery.get("www.html?_=" + Math.random(),function(response){
				jQuery("#cnt").html(response);
			});
			return;
			var fragment = document.createDocumentFragment();  
			var div = document.createElement("div");
			var _html = '';
			var xmlhttp = new XMLHttpRequest();
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					document.getElementById("cnt").innerHTML = xmlhttp.responseText;
					
					var scripts = document.getElementById("cnt").getElementsByTagName("script");
					/*
					jQuery.each(scripts,function(i,item){
						var el = document.createElement("script");
						if(item.src){
							el.src = item.src;
							item.parentNode.replaceChild(el, item);
						}
						 
					});
					*/
					excScript(scripts);
				}
			};
			xmlhttp.open("GET","www.html",true);
			xmlhttp.send(null);
			
		});
		
	</script>
</body>
</html>