JS Code Reference location Problem-Go

Source: Internet
Author: User

See a lot of JS code all put in the head of the situation, in fact, this is a detail problem. Reprint a user to know the answer to the river: in the river
Links: https://www.zhihu.com/question/34147508/answer/63469443
Source: Know

The browser loading page is loaded from top to bottom. When you load JS and execute it, it blocks the loading of other resources. This is because JS may have DOM, style, and so on, so the browser to load this JS and execute, and then load the HTML, CSS, which is placed behind it.

Therefore, add a huge JS on the top, the browser first to download and execute, during this time, the page is blank. Rather than loading some HTML and styling without the JS interaction, it is clear that the latter is better for the visitors ' experience.However, are all JS to be placed at the bottom? Not.

To cite practical examples:

Html5shivis a JS script, used to be early IE compatible HTML5 new tag support.

This JS script must be placed in the head section above, why? Because under the old IE browser, do not support <section> such tags, so with JS to be compatible. If placed at the bottom, and so on IE browser first error, will execute this JS not much use.

The library of JQuery is also recommended on the top. First of all, perhaps the biggest question is that jQuery is so big that it's not so clogged up on top?

However, jquery usually refers to a CDN address, and after gzip compression is not very large, and reference to the public CDN above the jQuery, a large probability has long been cached by the user. So you don't have to think about the performance issues with blocking.

And as we all know, JS is executed in order. That is, you run the code that calls the jquery function before you run jquery, and you get an error. You can control your own write JS placed at the bottom of the page, placed under JQuery, but you can not guarantee that the other places on the page will not be able to save the diagram directly in the HTML code output JS. If so, you will get an error.

So I suggest thatThe class library is loaded on top and the rest of the code is loaded at the bottom as much as possible.

JS Code Reference location Problem-Go

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.