原理解析:第一步,用 DOM 建立 <script> 或 <link> 標籤,並為它們附加 type 等屬性;第二步,用 appendChild 方法把標籤綁定到另一個標籤上,通常是綁到 <head>。
應用場景:1、提高程式碼復用、減少程式碼量;2、加入一個 JavaScript 控制器與 session,即可動態改變頁面樣式;3、由於頁面是從上到下依次加載並逐行解釋,因此可以透過控制器控制載入順序,例如先載入 CSS 布局檔,再顯示含圖片的美化檔,之後再載入大的 Flash,或依內容重要性排序載入。
閱讀提示:英文不好的初學者可直接看中文,再拷貝程式碼實驗。
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> 標籤。綁定時有個問題:同一個文件有可能被綁定兩次。綁定兩次瀏覽器雖不會出錯,但效率較低。為避免這種情況,我們可以新增一個全局陣列變數,把已綁定的文件名保存進去,每次綁定前先檢查是否已存在:若存在就提示,不存在才綁定。
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:
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:
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.