动态加载、移除、替换JS和CSS
2020-11-18 07:50
标签:style java c t tar javascript sp ext get 文件 set //动态加载一个js/css文件 动态加载、移除、替换JS和CSS,搜素材,soscw.com 动态加载、移除、替换JS和CSS 标签:style java c t tar javascript sp ext get 文件 set 原文地址:http://www.cnblogs.com/ITanyx/p/3701047.html
function loadjscssfile(filename, filetype) {
if (filetype == "js") {
var fileref =
document.createElement(‘script‘)
fileref.setAttribute("type", "text/javascript")
fileref.setAttribute("src", filename)
}
else
if (filetype == "css") {
var fileref =
document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if
(typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
//移动已经加载过的js/css
function
removejscssfile(filename, filetype) {
var targetelement =
(filetype == "js") ? "script" : (filetype == "css") ? "link" : "none"
var targetattr = (filetype == "js") ? "src" : (filetype ==
"css") ? "href" : "none"
var allsuspects =
document.getElementsByTagName(targetelement)
for (var i =
allsuspects.length; i >= 0; i--) {
if (allsuspects[i] &&
allsuspects[i].getAttribute(targetattr) != null &&
allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
allsuspects[i].parentNode.removeChild(allsuspects[i])
}
}