<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-9  評論-168  文章-266  trackbacks-0
    動態加載外部CSS與JS文件使用dom創建<script>或者<link>標簽,并給他們附加屬性,如type等。然后使用appendChild方法把標簽綁定到另一個標簽,一般是綁到<head>。
      應用:
      1、提高代碼的復用,減少代碼量;
      2、添加一個javascript控制器和 session可以實現動態改變頁面樣式;
      3、由于是頁面是從上到下依次加載文件的,并且邊加載邊解釋,所以可以添加javascript控制器控制頁面文件的加載順序,如先加載css布局文件,再顯示有圖片的css美化文件,之后再加載大的falsh文件,或者安內容的重要性來加載。

       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>標簽。綁定的時候有一個問題就是同一個文件有可能被我們綁定兩次,綁定兩次瀏覽器也不會出現異常,但是效率就低了。為了避免
      這種情況我們可以新增一個全局數組變量,把綁定的文件名字保存在里面,每次綁定前先檢查一下是否已經存在,如果存在就提示已經存在,如果不存在就綁定。

    div css xhtml xml Example Source Code Example Source Code [www.52css.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:

    div css xhtml xml Example Source Code Example Source Code [www.52css.com]
    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. 

    div css xhtml xml Example Source Code Example Source Code [www.52css.com]
    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 文件 
    posted on 2009-12-25 15:58 紫蝶∏飛揚↗ 閱讀(735) 評論(0)  編輯  收藏 所屬分類: div+css+js代碼
    主站蜘蛛池模板: 亚洲蜜芽在线精品一区| 妇女自拍偷自拍亚洲精品| 久久九九免费高清视频| 免费大片黄手机在线观看| 美女露隐私全部免费直播| 亚洲国产精品尤物YW在线观看 | 亚洲免费黄色网址| 亚洲最大成人网色| 中文毛片无遮挡高潮免费| 久久久久亚洲国产| 日韩成人免费aa在线看| 黄色网页免费观看| 最新精品亚洲成a人在线观看| a级毛片毛片免费观看久潮喷| 久久精品亚洲综合专区| 曰批全过程免费视频网址| 国产人成亚洲第一网站在线播放| 在线观看人成视频免费| 免费精品视频在线| 亚洲国产精品VA在线看黑人| 亚洲一级毛片免费观看| 亚洲精品日韩一区二区小说| 亚洲日本中文字幕一区二区三区| 在线观看肉片AV网站免费| 亚洲一区免费在线观看| 国产高清免费观看| 国产日韩AV免费无码一区二区| 亚洲精品在线电影| gogo全球高清大胆亚洲| 久久久久久国产精品免费免费男同 | 亚洲av日韩av综合| 免费国产怡红院在线观看| 你懂的免费在线观看| 456亚洲人成影院在线观| 亚洲熟女乱综合一区二区| 久久久精品2019免费观看| 精品韩国亚洲av无码不卡区| 久久亚洲精品无码| 四虎影在线永久免费四虎地址8848aa| 国产午夜无码精品免费看动漫| 亚洲欧美日韩中文高清www777|