JS and CSS in the webpage head tag, which file is placed in front and which is better in the back? -

Source: Internet
Author: User
Tags prefetch
0 reply content: the articles by Yubo and Ke Jun mainly address the question about the resource loading sequence. I will provide some answers to the questions. The order is generally:

1. Some special JavaScript codes, such as basic scripts used for debugging (not necessarily available during deployment) and Performance Logs, must be placed at the very beginning as far as possible.
2. External style sheet (link [rel = stylesheet])
3. style on this page)
4. Basic libraries, such as loader, various shim/polyfill, and jQuery
Note: Some developers tend to add defer or put it at the bottom of the page from the perspective of performance optimization. However, not all scripts can do this. For example, the html5-shim script must be loaded before the body. Another example is the compatibility Implementation of the history api, which should not be defer, because you cannot ensure that the user has no back/forward action before the page ready. Another example is jQuery and defer, but it also means that all the functions that depend on jQuery need defer. Considering that these static files are usually cached, therefore, not defer is not an acceptable compromise.
5. A small number of scripts on this page

Above. Followed by the guidance of @ ou lei, Zhang kejun's blog mentioned another in-depth article: http://lifesinger.wordpress.com/2012/02/03/performance-impact-of-js-css-loading-order/

------------------------ Due to http://wordpress.com This website does not exist. The following content is fabricated through the Pocket (formerly ReadItLater) service. Please refer to the official self-identification -----------------------

Effect of JS and CSS locations on loading sequence of other resources
Ke Jun did a series of tests: the sequential relationship between js and css, and gave the phenomenon and conclusion, but did not give the reason.
The position of JS and CSS on the page affects the loading sequence of other resources (non-js and css resources such as img). There are three notable reasons:
  1. Javascript may modify the DOM.Typically, there may be document. write. This means that subsequent download of all resources may be unnecessary before the current JS loading and execution is complete. This is the root cause of JS blocking subsequent resource downloads.
  2. Javascript execution may depend on the latest style.For example, there may be var width = $ ('# id '). width (). this means that before JavaScript code is executed, the browser must ensure that all css (both external and embedded) before this JS Code has been downloaded and parsed. This is the root cause of CSS blocking subsequent JavaScript Execution.
  3. Modern browsers are smart and will optimize prefetch.Performance is so important. In the competition of modern browsers, in addition to the UI update thread, another thread will be enabled to download JS and CSS in advance (Note: Only download in advance, not executed ). With prefetch optimization, this means that in the absence of any blocking, theoretically, the download time of JS and CSS is very high and has nothing to do with the location.

The preceding three rules are briefly described as follows:
  • Law 1: whether resources are downloaded depends on JS execution results.
  • Law 2: javascript execution depends on the latest CSS rendering.
  • Law 3: modern browsers have prefetch optimization.

With these three laws, let's take a look at KE Jun's test, which is very clear:

The external js appears in a and B-head. No matter how it is put, the css file cannot be in parallel with the request in the body.

According to Law 1 and Law 3, we can see that the above conclusions are not correct. For example:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.