动态性载入外界css或js文档

2021-03-16 01:37 jianzhan

基本原理分析:第1步:应用dom建立<script>或<link>标识,并给她们额外特性,如type等第2步:应用appendChild方式把标识关联到另外一个标识,1般是绑到<head>.
运用:1、提升编码的复用,降低编码量;2、加上1个javascript操纵器和 session能够完成动态性更改网页页面款式;3、因为是网页页面是从上到下先后载入文档的,而且边载入边解释,因此能够加上javascript操纵器操纵网页页面文档的载入次序,如先载入css合理布局文档,再显示信息有照片的css清理文档,以后再载入大的falsh文档,或安內容的关键性来载入。\r
阅读文章提醒:e文不太好的初学者能够立即看汉语,随后复制编码实验下。\r
To load a .js or .css file dynamically, in a nutshell, it means using DOM methods to first create a swanky new "script" or "LINK" element, assign it the appropriate attributes, and finally, use element.appendChild() to add the element to the desired location within the document tree. It sounds a lot more fancy than it really is. Lets see how it all comes together:
下列为引入的內容:

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", "css") //开启网页页面时访问器动态性的载入.css 文档
接下来的工作中是关联到<head>标识。关联的情况下有1个难题便是同1个文档有将会被大家关联两次,关联两次访问器也不容易出現出现异常,可是高效率就低了。以便防止\r
这类状况大家能够新增1个全局性数字能量数组自变量,把关联的文档姓名储存在里边,每次关联前先查验1下是不是早已存在,假如存在就提醒早已存在,假如不存在就关联。\r
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 "]")==⑴){// indexOf分辨数字能量数组里是不是有某1项
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.