中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

動態加載外部css或js文件

2022-10-30    分類: 網站建設

動態加載外部css或js文件

原理解析:第一步:使用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, 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:

接下來的工作是綁定到<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") //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.

以下是引用片段:
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 文件

網站標題:動態加載外部css或js文件
文章來源:http://m.2m8n56k.cn/news30/209530.html

成都網站建設公司_創新互聯,為您提供網站內鏈、網站營銷、響應式網站網站設計用戶體驗建站公司

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網頁設計公司
主站蜘蛛池模板: 天天拍拍夜夜出水 | a黄视频 | 国产在线精品二区韩国演艺界 | 欧美xxx高清| 国产欧美一区二区三区在线看 | 国内精品久久久久久久久蜜桃 | 综合视频在线 | 亚洲视屏在线 | 在线看毛片网站 | 亚洲综合精品一二三区在线 | 黄视频欧美 | 久久精品午夜视频 | 一个人看的日本免费视频 | 欧美成人免费全部色播 | 又黄又爽又刺激的视频 | 国产精品久久国产三级国不卡顿 | 免费看又黄又爽又猛的网站 | 日韩高清在线二区 | 色偷偷亚洲男人天堂 | 亚洲 [12p]| 欧美h版成版在线观看 | 欧美精品久久久久久久影视 | 中文字幕一区二区三 | 亚洲图片一区二区三区 | 亚洲天堂久久新 | 国产精品情侣久久婷婷文字 | 99re66热这里只有精品免费观看 | 欧美国产在线一区 | 免费逼片 | 萌白酱福利视频在线网站 | 国产日产精品_国产精品毛片 | 免费a级毛片视频 | 暴操女人| 日韩欧美一级毛片视频免费 | 久草a在线 | 亚洲欧美日韩在线观看二区 | 91亚洲精品一区二区福利 | 韩国日本一级毛片免费视频 | 国产国语在线播放视频 | a男人的天堂久久a毛片 | 国产精品久久大陆 |