djscssfile(), 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.
function loadjscssfile(filename, filetype){ if (filetype=="js"){ //判断文件类型 var fileref=document.createElement(''script'')//创建标签 fileref.setAttribute("type","text/javascript")//定义属性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) } loadjscssfile("myscript.js", "js") //打开页面时浏览器动态的加载文件 loadjscssfile("javascript.php", "js") // 打开页面时浏览器动态的加载"javascript.php" , loadjscssfile("mystyle.css",
|