Where should I declare JavaScript files used in my page? In <head></head> or near </body>?

后端 未结 12 910
后悔当初
后悔当初 2020-11-27 15:07

I was reading a tutorial and the author mentioned to include Javascript files near closing body tag () in HTML.

I was wonderi

相关标签:
12条回答
  • 2020-11-27 15:40

    You should do it near </body>. The reason is simple: If you place it into the head area, the files must be loaded before the body area can be. For that time, the user just sees a white screen.

    But it depends on your website. I would load frameworks like mootools in the head area, other functions for events or AJAX or something should be loaded near </body>.

    0 讨论(0)
  • 2020-11-27 15:44

    I like to load a small js file in the head, that handles (1) anything that happens before the page is rendered and (2) the loading of other script files after the page loads, or as needed.

    0 讨论(0)
  • 2020-11-27 15:44

    You should put JavaScript right before </body>. Ideally, your HTML should function without JavaScript, so it should be one of the last things loaded.

    Bear in mind that you should use CSS to hide elements and not JavaScript. This avoids seeing elements appear and disappear as the page loads.

    You may also come across the following problem...

    Problem

    In this scenario, I'm going to use PHP as an example.

    Your footer.php file may currently look like this:

    <script src="jquery.js"></script>
    <script src="custom.js"></script>
    </body>
    </html>
    

    But what happens on the rare occasions you want to add some <script> exclusively for one page? You wouldn't be able to put it after footer.php because you wouldn't be in the <body> tag anymore, but you can't put it before, because then you'll be missing the jquery.js from your code.

    Solution

    Have a footer-start.php file:

    <script src="jquery.js"></script>
    <script src="custom.js"></script>
    

    And a footer-end.php file:

    </body>
    </html>
    

    And have your footer.php be simply:

    <?php
      require 'footer-start.php';
      require 'footer-end.php';
    

    Then, on the rare occasions that you need to use a custom <script> for one page, do this:

    <?php require 'footer-start.php'; ?>
    <script>...</script>
    <?php require 'footer-end.php'; ?>
    

    Doing it this way means you don't have to change all your previous code where footer.php is referenced.

    0 讨论(0)
  • 2020-11-27 15:46

    Script tags should generally be in the head section. The exceptions are when they do significant immediate processing that should be delayed until as late as possible in the page load to avoid interfering with the page coming up, as with Google Analytics, or when the script tag's actual placement is a part of its behavior.

    0 讨论(0)
  • 2020-11-27 15:47

    By putting them in the <head/> you force the browser to download the files before it can render a page. That causes the perceived load time to slow down.

    By placing them in the footer, right before the closing body tag, the browser will not load them until it reaches that point in the parsing of the HTML. That means that the scripts will run later in the page load process but will not block the asset download and rendering process.

    Which works best is up to you and how you develop your code.

    0 讨论(0)
  • 2020-11-27 15:48

    The only reason for putting it near the end of the body, AFAIK, is to be able to execute the JavaScript after the web browser has parsed your HTML document. E.g. if your JavaScript deals with "all elements named hello", the browser needs to read the entire document before executing your JavaScript. Makes sense, right?

    In e.g. JQuery, you can put your JavaScript anywhere in your document and use:

    $(document).ready(function () {
      // your code here
    });
    

    ...to make sure the entire document has been loaded into the DOM before executing the inner function. Of course, this can be done with normal JavaScript as well, but be careful not to break compatibility with some browsers, because their needs tend to differ a lot.

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