原理解析:第一步:使用dom建立<script>或<link>標籤,並給他們附加屬性,如type等第二步:使用appendChild方法把標籤綁定到另一個標籤,一般是綁在<head >.
應用:
1、提高程式碼的複用,減少程式碼量;
2、新增一個javascript控制器和session可以實現動態改變頁面樣式;
3、由於是頁面是從上到下依序載入檔案的,並且邊加載邊解釋,所以可以添加javascript控制器控制頁面文件的加載順序,如先加載css佈局文件,再顯示有圖片的css美化文件,之後再加載大的falsh文件,或者安內容的重要性來加載。
閱讀提示:e文不好的初學者可以直接看中文,然後拷貝代碼試驗下。
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, end 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>標籤。綁定的時候有一個問題就是同一個檔案有可能被我們綁定兩次,綁定兩次瀏覽器也不會出現異常,但是效率就低了。為了避免
這種情況我們可以新增一個全域數組變量,把綁定的檔案名字保存在裡面,每次綁定前先檢查一下是否已經存在,如果存在就提示已經存在,如果不存在就綁定。
以下是引用片段:
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 a that no existing el 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 Javascript file. This is problematic only from an Javascript file. This 是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 prevent the same file from being added more than once is to preventd of the filescsed, files )( 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 notd, thatmos
that the you actually remove or replace an added .js or .css file. Lets see how that's done next.