JavaScript in <head> or just before </body>?

前端 未结 6 2192
攒了一身酷
攒了一身酷 2020-11-28 06:14

I am about to embark on a new web project and I plan to put some JavaScripts in the and also some before , using the foll

相关标签:
6条回答
  • 2020-11-28 06:35

    One of the reasons you'd want to put scripts before the </body> is if they manipulate the DOM without user interaction, so you'll need the DOM to be loaded in order to be manipulated. Another way to do that is to add an event listener and run the scripts when the page has loaded, but this will require additional code, which might get complicated if you have a lot of scripts, especially ones you haven't written yourself. Putting them at the end of the page also will speed up page load, though in the case of DOM manipulating scripts you might get some not-so-pretty results from that.

    0 讨论(0)
  • 2020-11-28 06:43

    Put the Javascript in a seperate file and place a link to it in the head part of HTML.

    0 讨论(0)
  • 2020-11-28 06:44

    I'd say that's perfectly sensible. As you said, as long as you don't move essential scripts (e.g. jQuery, Modernizr, etc etc) out from the <head>, you shouldn't have problems.

    Moving non-essential scripts to the bottom of the page should help with the perceived loading speed (that and minimizing / concatenating scripts).

    0 讨论(0)
  • 2020-11-28 06:44

    It all depends on what you mean by "essential for UX". I agree with having modernizr appear early for example, but not everything needs to load straight away. If you're trying to avoid a flash of unstyled text (FOUT), that's a good reason. Similarly, if you have scripts that affect how the page looks before the user does anything, you should load those early.

    Don't forget though, speed is part of UX. There's no advantage in having some jquery interaction ready to run when the user can't see the content it applies to yet. The difference between loading the scripts at the start of the end is a matter of seconds. If you let the page load first, the user will be using those seconds to take the page in, allowing you to load scripts unobtrusively.

    Your page will load faster if you move scripts to the bottom of the page, and that makes a difference to your pagerank these days.

    Also, some versions of IE will throw errors if you try to run a script before the element it refers to has loaded.

    Like Ed says, your scripts should be stored in a separate file, and in as few files as possible.

    0 讨论(0)
  • 2020-11-28 06:49

    Javascript should be placed at the end of the document so that it doesn't delay the parallel loading of page elements. This does then require that the js is written in a specific way but it does improve the speed of page loads.

    Also, ideally you could host references like this under a different (sub)domain. References to jquery should be pointed to googles CDN too.

    See http://developer.yahoo.com/performance/rules.html for more info.

    0 讨论(0)
  • 2020-11-28 06:54

    I think a lot of developers run javascript just before the </body> so that it is ran after all the elements have been rendered.

    However, if you organise your code correctly, the position on the page doesn't matter.

    For example, when using jQuery, you can ensure the code isn't ran until the page and its elements are fully rendered by doing the following:

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

    Then the script reference can be put in the head tag.


    Update - Script tags should be referenced just before </body>. This prevents render blocking while the scripts load and is much better for site perception speed.

    No obtrusive javascript should be used when using this technique.

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