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

后端 未结 12 909
后悔当初
后悔当初 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:23

    I believe it's better to place script tags just before the closing body tag. Because:

    • Elements are blocked from rendering if they are below the script.
    • In IE6, IE7 resources in the page are blocked from downloading if they are below the script.

    From this article. Also Yahoo's performance rule 6 is Move Scripts to the Bottom

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

    It will often be argued that for speed purposes you should put script tags right at the end of the document (before the close body tag). While this will result in the fastest page load, it has some serious downsides.

    Firstly, a common idiom with Webpage development is to have a header file, a footer file and your content in the middle. To keep unnecessary Javascript to a minimum, you'll often want to put code snippets in individual pages.

    If you include jquery, for example, at the end of the document, your jquery code snippets (like document ready stuff) must happen after that. That can be awkward from a development point of view.

    Secondly, in my experience, because the page load is faster, you can end up noticing certain effects being applied because the page has already loaded by the time they are applied.

    For example if you put a table in a document and right before the body close tag put:

    $(function() {
      $("tr:nth-child(odd)").addClass("odd");
    });
    

    with appropriate styling, that effect being applied will often be visible. Personally I think that makes for a bad user experience potentially. I think often you're better off having the page load slightly slower (by putting scripts at the top) if you don't get disconcerting visual effects.

    I generally advocate effective caching strategies so you only have to download Javascript files when they change, as in Supercharging Javascript in PHP (but the principles apply to any language, not just PHP) and still putting scripts at the top. It's far more convenient.

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

    The Place of the <script> Element

    The script elements block progressive page downloads.
    Browsers download several components at a time, but when they encounter an external script, they stop further downloads until the script file is downloaded, parsed, and executed.
    This hurts the overall page time, especially if it happens several times during a page load.
    To minimize the blocking effect, you can place the script element toward the end of the page, right before the closing tag.
    This way there will be no other resources for the script to block. The rest of the page components will be downloaded and already engaging the user.
    The worst antipattern is to use separate files in the head of the document:

    <!doctype html>
    <html>
    <head>
        <title>My App</title>
        <!-- ANTIPATTERN -->
        <script src="jquery.js"></script>
        <script src="jquery.quickselect.js"></script>
        <script src="jquery.lightbox.js"></script>
        <script src="myapp.js"></script>
    </head>
    <body>
    ...
    </body>
    </html>
    

    A better option is to combine all the files:

    <!doctype html>
    <html>
    <head>
        <title>My App</title>
        <script src="all_20100426.js"></script>
    </head>
    <body>
        ...
    </body>
    </html>
    

    And the best option is to put the combined script at the very end of the page:

    <!doctype html>
    <html>
    <head>
        <title>My App</title>
    </head>
    <body>
        ...
        <script src="all_20100426.js"></script>
    </body>
    

    “JavaScript Patterns, by Stoyan Stefanov (O’Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750.”

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

    The Yahoo YSlow tool has advice on this:

    The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

    In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

    An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.

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

    The reason for declaring near the end is that your page can begin drawing before having to wait to fetch the .js.

    Ergo, stuff you would want at the end would have no effect on the page rendering, and vice versa.

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

    Google pagespeed have some nice explanation on how to parallelize downloading of scripts.

    Still their advice is to put them in the head of your page.

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