天府星空网络科技成都网站制作公司,专业从事成都网站建设成都网页制作、成都网站设计以精深技术为核心、以专业队伍为支撑。致力于为成都政府网站建设成都公司网站建设成都学校网站建设等提供高技术含量的成都网站设计服务。凭借多年的成都网页设计成功经验,公司已拥有数百例经典网站案例,是一家正规经营,诚实守信的专业成都网站建设公司

动态加载外部css或js文件

作者:佚名  来源:本站整理  发布时间:2008-1-23 9:44:22
查一下是否已经存在,如果存在就提示已经存在,如果不存在就绑定。

以下是引用片段:
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.

上一页  [1] [2] 

【公司简介】:
   天府星空网络科技成都网站制作公司,专业从事成都网站建设 成都网页制作 成都网站设计以精深技术为核心、以专业队伍为支撑。致力于为成都政府网站建设成都公司网站建设成都学校网站建设等提供高技术含量的成都网页设计服务。
   公司始终以不懈的努力、更高的目标来要求自己。凭借多年的成都网页设计成功经验,公司已拥有数百例经典网站成功案例,是正规的成都网站建设公司
   成都网站建设的专业服务商,强大的成都网页制作技术团队,在网站设计等方面始终保持领先地位,并获得了网页制作公司业界的广泛赞誉和认同。公司重视网页制作质量,打造成都地区网站制作公司知名品牌,以优秀的网站设计技术,创一流的成都网站建设作品。建网站就找天府星空,助您马到成功!