亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript中延遲加載的7種方法實現(xiàn)

 更新時間:2025年01月13日 11:10:55   作者:baoeni  
在web前端開發(fā)中,性能優(yōu)化一直是一個非常重要的話題,JavaScript中延遲加載的方式有很多種,本文就來介紹了javascript中延遲加載的7種方法實現(xiàn),具有一定的參考價值,感興趣的可以了解一下

延遲加載javascript,也就是頁面加載完成之后再加載javascript,也叫on demand(按需)加載,一般有一下幾個方法:

1. DOM

head append script tag

window.onload = function() {
    setTimeout(function(){

        // reference to <head>
        var head = document.getElementsByTagName('head')[0];

        // a new CSS
        var css = document.createElement('link');
        css.type = "text/css";
        css.rel = "stylesheet";
        css.href = "new.css";

        // a new JS
        var js = document.createElement("script");
        js.type = "text/javascript";
        js.src = "new.js";

        // preload JS and CSS
        head.appendChild(css);
        head.appendChild(js);

        // preload image
        new Image().src = "new.png";

    }, 1000);
};

2. document.write

<script language="javascript" type="text/javascript">
        function include(script_filename) {
            document.write('<' + 'script');
            document.write(' language="javascript"');
            document.write(' type="text/javascript"');
            document.write(' src="' + script_filename + '">');
            document.write('</' + 'script' + '>');
        }

        var which_script = '1.js';
        include(which_script);
        </script>

3. Iframe

和第一種類似,但是script tag是放到iframe的document里面。

window.onload = function() {
    setTimeout(function(){

        // create new iframe
        var iframe = document.createElement('iframe');
        iframe.setAttribute("width", "0");
        iframe.setAttribute("height", "0");
        iframe.setAttribute("frameborder", "0");
        iframe.setAttribute("name", "preload");
        iframe.id = "preload";
        iframe.src = "about:blank";
        document.body.appendChild(iframe);

        // gymnastics to get reference to the iframe document
        iframe = document.all ? document.all.preload.contentWindow : window.frames.preload;
        var doc = iframe.document;
        doc.open(); doc.writeln("<html><body></body></html>"); doc.close(); 

        // create CSS
        var css = doc.createElement('link');
        css.type = "text/css";
        css.rel = "stylesheet";
        css.href = "new.css";

        // create JS
        var js = doc.createElement("script");
        js.type = "text/javascript";
        js.src = "new.js";

        // preload CSS and JS
        doc.body.appendChild(css);
        doc.body.appendChild(js);

        // preload IMG
        new Image().src = "new.png";

    }, 1000);
};

4. Iframe static page

直接把需要加載東西放到另一個頁面中

window.onload = function() {
    setTimeout(function(){

        // create a new frame and point to the URL of the static
        // page that has all components to preload
        var iframe = document.createElement('iframe');
        iframe.setAttribute("width", "0");
        iframe.setAttribute("height", "0");
        iframe.setAttribute("frameborder", "0");
        iframe.src = "preloader.html";
        document.body.appendChild(iframe);

    }, 1000);
};

5. Ajax eval

用ajax下載代碼,然后用eval執(zhí)行

6. Ajax Injection

用ajax下載代碼,建立一個空的script tag,設(shè)置text屬性為下載的代碼

7. async 屬性(缺點是不能控制加載的順序)

<script src="" async="true"/>

到此這篇關(guān)于javascript中延遲加載的7種方法實現(xiàn)的文章就介紹到這了,更多相關(guān)javascript 延遲加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JS 如果改變span標簽的是否隱藏屬性

    JS 如果改變span標簽的是否隱藏屬性

    JS 如果改變span標簽的是否隱藏屬性,大家根據(jù)需要選擇使用。
    2011-10-10
  • WEEX環(huán)境搭建與入門詳解

    WEEX環(huán)境搭建與入門詳解

    這篇文章主要介紹了WEEX環(huán)境搭建與入門詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • 一文詳解JavaScript中this指向的問題

    一文詳解JavaScript中this指向的問題

    JavaScript中this指向的問題是面試中常常會問到的,所以本文就來通過一些簡單的示例為大家詳細講講,文中的示例代碼講解詳細,需要的可以參考一下
    2023-04-04
  • js實現(xiàn)右鍵菜單欄功能

    js實現(xiàn)右鍵菜單欄功能

    這篇文章主要為大家詳細介紹了js實現(xiàn)右鍵菜單欄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • JS實現(xiàn)移動端觸屏拖拽功能

    JS實現(xiàn)移動端觸屏拖拽功能

    這篇文章主要介紹了JS實現(xiàn)移動端觸屏拖拽功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • 詳解async/await 異步應(yīng)用的常用場景

    詳解async/await 異步應(yīng)用的常用場景

    這篇文章主要介紹了詳解async/await 異步應(yīng)用的常用場景,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 詳細聊聊對async/await的理解和用法

    詳細聊聊對async/await的理解和用法

    隨著Nodev7的發(fā)布,越來越多的人開始研究據(jù)說是異步編程終級解決方案的 async/await,這篇文章主要給大家介紹了關(guān)于對async/await的理解和用法,文中通過實例代碼介紹的介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • javascript實現(xiàn)去除HTML標簽的方法

    javascript實現(xiàn)去除HTML標簽的方法

    這篇文章主要介紹了javascript實現(xiàn)去除HTML標簽的方法,涉及javascript正則替換相關(guān)操作技巧,需要的朋友可以參考下
    2016-12-12
  • js使用心得分享

    js使用心得分享

    這里給大家分享的是本人近期學習與使用javascript之后總結(jié)出來的一些經(jīng)驗和心得,雖然目前只有5點,但以后會慢慢更新,希望對大家能有所幫助。
    2015-01-01
  • 取得窗口大小 兼容所有瀏覽器的js代碼

    取得窗口大小 兼容所有瀏覽器的js代碼

    我們首先把window.innerWidth和window.innerHeight的值分別付給了pageWidth和pageHeight。
    2011-08-08

最新評論