教你如何動(dòng)態(tài)加載外部CSS與JS文件_Web標(biāo)準(zhǔn)教程
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!
動(dòng)態(tài)加載外部CSS與JS文件使用dom創(chuàng)建<script>或者<link>標(biāo)簽,并給他們附加屬性,如type等。然后使用appendChild方法把標(biāo)簽綁定到另一個(gè)標(biāo)簽,一般是綁到<head>。
應(yīng)用:
1、提高代碼的復(fù)用,減少代碼量;
2、添加一個(gè)javascript控制器和 session可以實(shí)現(xiàn)動(dòng)態(tài)改變頁(yè)面樣式;
3、由于是頁(yè)面是從上到下依次加載文件的,并且邊加載邊解釋?zhuān)钥梢蕴砑觠avascript控制器控制頁(yè)面文件的加載順序,如先加載css布局文件,再顯示有圖片的css美化文件,之后再加載大的falsh文件,或者安內(nèi)容的重要性來(lái)加載。
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:
接下來(lái)的工作是綁定到<head>標(biāo)簽。綁定的時(shí)候有一個(gè)問(wèn)題就是同一個(gè)文件有可能被我們綁定兩次,綁定兩次瀏覽器也不會(huì)出現(xiàn)異常,但是效率就低了。為了避免
這種情況我們可以新增一個(gè)全局?jǐn)?shù)組變量,把綁定的文件名字保存在里面,每次綁定前先檢查一下是否已經(jīng)存在,如果存在就提示已經(jīng)存在,如果不存在就綁定。
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.
應(yīng)用:
1、提高代碼的復(fù)用,減少代碼量;
2、添加一個(gè)javascript控制器和 session可以實(shí)現(xiàn)動(dòng)態(tài)改變頁(yè)面樣式;
3、由于是頁(yè)面是從上到下依次加載文件的,并且邊加載邊解釋?zhuān)钥梢蕴砑觠avascript控制器控制頁(yè)面文件的加載順序,如先加載css布局文件,再顯示有圖片的css美化文件,之后再加載大的falsh文件,或者安內(nèi)容的重要性來(lái)加載。
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:
接下來(lái)的工作是綁定到<head>標(biāo)簽。綁定的時(shí)候有一個(gè)問(wèn)題就是同一個(gè)文件有可能被我們綁定兩次,綁定兩次瀏覽器也不會(huì)出現(xiàn)異常,但是效率就低了。為了避免
這種情況我們可以新增一個(gè)全局?jǐn)?shù)組變量,把綁定的文件名字保存在里面,每次綁定前先檢查一下是否已經(jīng)存在,如果存在就提示已經(jīng)存在,如果不存在就綁定。
示例代碼 [m.dounai2.com]
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:
示例代碼 [m.dounai2.com]
var filesadded="" //保存已經(jīng)綁定文件名字的數(shù)組變量
function checkloadjscssfile(filename, filetype){
if (filesadded.indexOf("["+filename+"]")==-1){// indexOf判斷數(shù)組里是否有某一項(xiàng)
loadjscssfile(filename, filetype)
filesadded+="["+filename+"]" //把文件名字添加到filesadded
}
else
alert("file already added!")//如果已經(jīng)存在就提示
}
checkloadjscssfile("myscript.js", "js") //success
checkloadjscssfile("myscript.js", "js") //redundant file, so file not added
function checkloadjscssfile(filename, filetype){
if (filesadded.indexOf("["+filename+"]")==-1){// indexOf判斷數(shù)組里是否有某一項(xiàng)
loadjscssfile(filename, filetype)
filesadded+="["+filename+"]" //把文件名字添加到filesadded
}
else
alert("file already added!")//如果已經(jīng)存在就提示
}
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.
示例代碼 [m.dounai2.com]
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //判斷文件類(lèi)型
var fileref=document.createElement('script')//創(chuàng)建標(biāo)簽
fileref.setAttribute("type","text/javascript")//定義屬性type的值為text/javascript
fileref.setAttribute("src", filename)//文件的地址
}
else if (filetype=="css"){ //判斷文件類(lèi)型
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") //打開(kāi)頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載文件
loadjscssfile("javascript.php", "js") // 打開(kāi)頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載"javascript.php" ,
loadjscssfile("mystyle.css", "css") //打開(kāi)頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載.css 文件
if (filetype=="js"){ //判斷文件類(lèi)型
var fileref=document.createElement('script')//創(chuàng)建標(biāo)簽
fileref.setAttribute("type","text/javascript")//定義屬性type的值為text/javascript
fileref.setAttribute("src", filename)//文件的地址
}
else if (filetype=="css"){ //判斷文件類(lèi)型
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") //打開(kāi)頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載文件
loadjscssfile("javascript.php", "js") // 打開(kāi)頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載"javascript.php" ,
loadjscssfile("mystyle.css", "css") //打開(kāi)頁(yè)面時(shí)瀏覽器動(dòng)態(tài)的加載.css 文件
相關(guān)Web標(biāo)準(zhǔn)教程:
- Web標(biāo)準(zhǔn):需要正確的使用H1~ H6標(biāo)簽
- 五個(gè)WEB標(biāo)準(zhǔn)中必須注意的地方
- 做到結(jié)構(gòu)(XHTML),表現(xiàn)(CSS),行為(JavaScript)的分離
- CSS標(biāo)準(zhǔn):HTML標(biāo)簽元素的關(guān)閉
- Web標(biāo)準(zhǔn)之路 勿使用W3C廢棄的元素
- CSS布局 聲明文檔類(lèi)型描述(DTD)
- DIV CSS\讓搜索引擎蜘蛛不再累
- 用XHTML+CSS創(chuàng)建基于Web標(biāo)準(zhǔn)的網(wǎng)頁(yè)
- XHTML+CSS創(chuàng)建基于Web標(biāo)準(zhǔn)的網(wǎng)站
- DIV+CSS對(duì)SEO的幫助
- 淺析什么是Web標(biāo)準(zhǔn) 及其前景
- CSSer新手寄語(yǔ):開(kāi)始標(biāo)準(zhǔn)之路
Web標(biāo)準(zhǔn)教程Rss訂閱Div+Css教程搜索
Web標(biāo)準(zhǔn)教程推薦
- CSS使用ul進(jìn)行網(wǎng)頁(yè)的多列布局
- WEB標(biāo)準(zhǔn)中國(guó)路會(huì)走向何方?
- DIV CSS:如何編寫(xiě)代碼才能更有效率
- DreamweaverCS3體驗(yàn)1
- WEB2.0標(biāo)準(zhǔn)教程:第五天 head區(qū)的其他設(shè)置
- 解決IE7以下版本不支持無(wú)A狀態(tài)偽類(lèi)的幾種方法!
- 大型Web2.0站點(diǎn)構(gòu)建技術(shù)初探
- CSS調(diào)試工具M(jìn)ultipleIE-IE5IE6IE7多版本共存
- 符合WEB標(biāo)準(zhǔn)的滾動(dòng)文字特效的實(shí)現(xiàn)方法
- 喧鬧過(guò)后迎來(lái)發(fā)展WEB2.0
猜你也喜歡看這些
- 相關(guān)鏈接:
- 教程說(shuō)明:
Web標(biāo)準(zhǔn)教程-教你如何動(dòng)態(tài)加載外部CSS與JS文件。