DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
如何動態載入外部 CSS 或 JS 文件
編輯:CSS詳解     

核心原理

動態載入外部資源的思路其實很直觀,可以拆成兩步:

第一步,用 DOM 方法創建一個 <script> 或 <link> 標籤,並為它附加必要的屬性,例如 type、src 或 href。

第二步,透過 appendChild 方法,把這個標籤綁定到另一個標籤上,通常是掛到 <head> 之下。

典型應用場景

1、提升代碼複用度,減少重複代碼量;

2、搭配一個 JavaScript 控制器與 session,即可動態切換頁面樣式;

3、由於頁面是從上到下依次載入並邊載入邊解析的,因此可以用 JavaScript 控制器來安排資源的載入順序——例如先載入承擔布局的 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:

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") // 重複文件,不再載入

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.

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved