DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> CSS入門知識 >> CSS詳解 >> 動態加載外部css或js文件
動態加載外部css或js文件
編輯:CSS詳解     

原理解析:第一步:使用dom創建<script>或者<link>標簽,並給他們附加屬性,如type等第二步:使用appendChild方法把標簽綁定到另一個標簽,一般是綁到<head>.

應用:1、提高代碼的復用,減少代碼量;2、添加一個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>標簽。綁定的時候有一個問題就是同一個文件有可能被我們綁定兩次,綁定兩次浏覽器也不會出現異常,但是效率就低了。為了避免\r

這種情況我們可以新增一個全局數組變量,把綁定的文件名字保存在裡面,每次綁定前先檢查一下是否已經存在,如果存在就提示已經存在,如果不存在就綁定。\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+"]")==-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.

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