innerHTML script

function parseScript(html)
{
	var result = { html:'',scripts:[] };
	if (html)
	{
		//脚本
		//var re = /(?:<script([^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig;
		var re = /(?:<script([^>]*text\/javascript[^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig;
		//外部脚本
		var srcRe = /\ssrc=([\'\"])(.*?)\1/i;
		var match;
		//
		while(match = re.exec(html))
		{
			var item = { src:'', text:''};
			//
			var attrs = match[1];
			var srcMatch = attrs ? attrs.match(srcRe) : false;
		    //外部
		    if(srcMatch && srcMatch[2]){
				item.src = srcMatch[2];
		    //内部
		    }else if(match[2] && match[2].length > 0){
				item.text = match[2];
		    }
			//
			result.scripts.push(item);	
		}
		result.html = html.replace(re, "");	
	}
	return result;
}


if (data = parseScript(data))
			{	
				if (data.html != '')
					main.innerHTML = data.html;
				for(var i=0;i<data.scripts.length;i++)
				{
					var item = data.scripts[i];
					if (item.text != '')
					{
						eval(item.text);
					}
					else if (item.src != '')
					{
						var tag = document.createElement('script');
						tag.setAttribute('type','text/javascript');
						tag.setAttribute('src', item.src);
						main.appendChild(tag);
					}					
				}
			}
此条目发表在javascript分类目录,贴了, 标签。将固定链接加入收藏夹。