Home  >  Article  >  Web Front-end  >  Javascript implements dynamic loading of CSS_javascript skills

Javascript implements dynamic loading of CSS_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:18:041036browse

Copy code The code is as follows:


A function written in JS is used to control the dynamic loading of JS files, that is, JS files are loaded only when needed, and CSS files can also be loaded, so that web pages can be reskinned. I think this function is well written. , take a serious look at it, you can see that it’s still quite good despite improving it.

Copy code The code 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 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 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;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn