动态加载外部css或js文件
以下是引用片段:
document.getElementsByTagName("head")[0].appendChild(fileref)
By referencing the HEAD element of the page first and then calling appendChild(), this means the newly created element is added to the very end of the HEAD tag. Furthermore, you should be aware that no existing element is harmed in the adding of the new element- that is to say, if you call loadjscssfile("myscript.js", "js") twice, you now end up with two new "script" elements both pointing to the same Javascript file. This is problematic only from an efficiency standpoint, as you'll be adding redundant elements to the page and using unnecessary browser memory in the process. A simple way to prevent the same file from being added more than once is to keep track of the files added by loadjscssfile(), and only load a file if it's new:
以下是引用片段:
var filesadded="" //保存已经绑定文件名字的数组变量
function checkloadjscssfile(filename, filetype){
if (filesadded.indexOf("["+filename+"]")==-1){// indexOf判断数组里是否有某一项
loadjscssfile(filename, filetype)
filesadded+="["+filename+"]" //把文件名字添加到filesadded
}
else
alert("file already added!")//如果已经存在就提示
}
checkloadjscssfile("myscript.js", "js") //success
checkloadjscssfile("myscript.js", "js") //redundant file, so file not added
Here I'm just crudely detecting to see if a file that's set to be added already exists within a list of added files' names stored in variable filesadded before deciding whether to proceed or not.
Ok, moving on, sometimes the situation may require that you actually remove or replace an added .js or .css file. Lets see how that's done next.
天府星空网络科技成都网站制作公司,专业从事成都网站建设 成都网页制作 成都网站设计以精深技术为核心、以专业队伍为支撑。致力于为成都政府网站建设、成都公司网站建设、成都学校网站建设等提供高技术含量的成都网页设计服务。
公司始终以不懈的努力、更高的目标来要求自己。凭借多年的成都网页设计成功经验,公司已拥有数百例经典网站成功案例,是正规的成都网站建设公司。
成都网站建设的专业服务商,强大的成都网页制作技术团队,在网站设计等方面始终保持领先地位,并获得了网页制作公司业界的广泛赞誉和认同。公司重视网页制作质量,打造成都地区网站制作公司知名品牌,以优秀的网站设计技术,创一流的成都网站建设作品。建网站就找天府星空,助您马到成功!
全国统一免费服务电话
800-82-98988
专业网站建设咨询电话
028-66533110
028-66533120
24小时移动总机
15902883120
天府星空05号:张小姐
负责范围:域名主机、网站建设
分机电话:15902883120转665
在线联络:QQ:239270001
电子邮件:sale@tfxk.com
负责范围:业务咨询、疑问解答
分机电话:15902883120转666
在线联络:QQ:239270002
电子邮件:sale@tfxk.com

您当前的位置: