动态加载javascript和css

 //动态加载js
function loadScript(url) {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild(script);
}

//动态加载css
function loadStyles(url) {
	var link = document.createElement('link');
	link.rel = 'stylesheet';
	link.type = 'text/css';
	link.href = url;
	document.getElementsByTagName('head')[0].appendChild(link);
}

//动态执行js
var script = document.createElement('script');
script.type = 'text/javascript';
var text = document.createTextNode("alert('Lee')"); //IE 浏览器报错
try{
	script.appendChild(text);
}catch(e){
    script.text = "alert('Lee')"
}
document.getElementsByTagName('head')[0].appendChild(script);

//动态执行css
var style = document.createElement('style');
style.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(style);
insertRule(document.styleSheets[0], 'body', 'background:red', 0);

function insertRule(sheet, selectorText, cssText, position) {
	if (sheet.insertRule) {//w3c
	    sheet.insertRule(selectorText + "{" + cssText + "}", position);
	} else if (sheet.addRule) {//ie
	    sheet.addRule(selectorText, cssText, position);
	}
}
原文地址:https://www.cnblogs.com/fangxuele/p/5430808.html