The code copy is as follows:
<script type="text/javascript">
function addStyle(stylePath) {
var container = document.getElementsByTagName("head")[0];
var addStyle = document.createElement("link");
addStyle.rel = "stylesheet";
addStyle.type = "text/css";
addStyle.media = "screen";
addStyle.href = stylePath;
container.appendChild(addStyle);
}
addStyle('http://www.xxx.com/wintys/dynamic.css');
</script>
The functions written by JS are used to control the dynamic loading of JS files, that is, the JS files are loaded when needed, and CSS files can also be loaded, so that web page skinning can be realized. I think this function is well written. If you take a closer look, it is still quite good after you improve it.
The code copy is as follows:
function $import(path,type,title){
var s,i;
if(!type) type=path.substr(path.lastIndexOf(".")+1);
if(type=="js"){
var ss=document.getElementsByTagName("script");
for(i=0;i<ss.length;i++){
if(ss[i].src && ss[i].src.indexOf(path)!=-1 || ss[i].title==title)return ss[i];
}
s=document.createElement("script");
s.type="text/javascript";
s.src=path;
if(title) s.title=title;
}
else if(type=="css"){
var ls=document.getElementsByTagName("link");
for(i=0;i<ls.length;i++){
if(ls[i].href && ls[i].href.indexOf(path)!=-1 || ls[i].title==title)return ls[i];
}
s=document.createElement("link");
s.rel="stylesheet";
s.type="text/css";
s.href=path;
if(title) s.title=title;
s.disabled=false;
}
else return;
var head=document.getElementsByTagName("head")[0];
head.appendChild(s);
return s;
}