Where to insert JavaScript Libraries and CSS in my HTML code?

前端 未结 4 1227
不思量自难忘°
不思量自难忘° 2021-01-16 00:44

I am little new to web development and when I was searching internet about other topics, I have seen many people has put popular JS Libraries in Different Places of

相关标签:
4条回答
  • 2021-01-16 01:32

    There is no so called "standard" method. The choice of where to put the lines boils down to one question: When will I need the library?

    You see, web files loads line by line, let's take the following as an example of what I mean:

    <script>
      document.getElementById("target").innerHTML = "changed"
    </script>
    <p id="target">unchanged</p>

    #target isn't altered because the script was loaded before the element did. Web files loads procedurally. When the line of JavaScript is loaded. It is executed immediately, but the target element isn't. So it couldn't change the element.

    Even with jQuery, there is the same problem:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
      $("#target").text("changed");
    </script>
    <p id="target">unchanged</p>

    Therefore, we often use $(function(){}).

    Back to the loading problem

    People who put their <script> tags or <link> tags in the body (in front) or in the head, wanted to execute the script immediately, sometimes they won't use $(function()) or document.onload

    People who put their <script> tags or <link> tags in the body (in the end) wanted to ensure all elements are loaded then execute the script or load CSS.

    Conclusion

    You should load independent resources such as jQuery first, then load dependent resources such as jQuery plugins. Then you try to decide when you want the resources to start loading, then put the lines in places.

    You should put CSS links in the <head> tag because you don't want visitors seeing unstyled content before loading the CSS files.

    If you can't decide or don't care about the time, put every <script> and <style> tags in the <head>.


    Here is another post you might be interested in: Load and execution sequence of a web page?

    0 讨论(0)
  • 2021-01-16 01:34

    CSS Sheets go in the < head >. The order of the CSS files matter so libraries should be put in first then you can put in the specific ones you have.

    Javascript links go in the < body > but place them at the very end. That way your HTML content loads first then the JS loads and it will recognize all your selections. It is more efficient to do it this way.

    0 讨论(0)
  • 2021-01-16 01:38

    The most important thing to note when placing your css and script tags is that the order you place them determines the order they are loaded in and if style or code is loaded later it over writes the code written before. So if you have css styling that assigns different styles to the same attributes of the same element then it is the one loaded later that takes effect. And with script tags it's important to remember that for dependency reasons. You should load the dependencies first so that they are there for the other scripts to use. Aside from that normally css tags are in the head and script tags at the bottom of your body element

    0 讨论(0)
  • 2021-01-16 01:42

    CSS can added inside header tag & but put all JS Libraries and custom files just before closing closing body tag

    <body>
     //other tags
    <script> All Scripts here </script>
    </body>
    
    • By doing so you wont have to check if DOM content has loaded.
    • It decrease page loading time.Otherwise a js need to be completely loaded before DOM loading.
    • It also makes sure that all events are attached properly to DOM element.

    I think this address all your concern specially the third one

    0 讨论(0)
提交回复
热议问题