頁面js和css載入順序最佳化建議__js

來源:互聯網
上載者:User

body裡dom渲染取決於head裡的外聯js載入完。

根據此原理,建議如下:

1、head裡出現外聯js,無論如何放,css檔案都不能和body裡的請求並行。

2、head裡面內聯js要在所有外聯css檔案前面,才能使css檔案和body裡面的請求並行,不然會堵塞。

因為內聯js要等head裡面所有外聯css載入完後才會執行。

3、外聯js放在頁面最後,進階瀏覽器會自動做最佳化。

一般是放在body裡面的最後幾行。

4、長執行的內聯js無論放在頁面任何位置都會影響頁面渲染。

長執行的js,比如監聽事件的代碼:

1 document.getElementById( 'bn' ).onclick = function () { alert(1); }

但這行代碼寫到外聯JS中就不影響。

參考文章:

https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery#example

https://developers.google.com/speed/docs/insights/BlockingJS

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.