When do you choose to load your javascript at the bottom of the page instead of the top?

后端 未结 9 1205
渐次进展
渐次进展 2020-12-10 17:15

I have seen JavaScript libraries being loaded at the top and bottom of the page.

I would love to know when to make these choices. All the JavaScript code I\'ve writt

相关标签:
9条回答
  • 2020-12-10 17:42

    I put a small script in the head that does anything I want done before the rest of the page renders, and loads any other required scripts onload, or as needed after the document loads.

    0 讨论(0)
  • 2020-12-10 17:46

    I put all external scripts (such as Google analytics) at the bottom so their lag does not effect the loading of the DOM.

    0 讨论(0)
  • 2020-12-10 17:48

    Because of the fact that browsers have to pause displaying content of a page when it's parsing a Javascript file, the recommendation is to load the Javascript at the bottom of the page to speed up displaying a page's content. This works best if your website can be rendered without any Javascript executing to modify the page because the page will be available for user interaction even if a script hangs for longer than expected.

    0 讨论(0)
  • 2020-12-10 17:48

    When the browser encounters a script element it has to evalute the JavaScript contained in that element because the script might alter the content of the page (via document.write) or inspect the current state of the page.

    If the script element loads script using the src attribute, loading of other resources (JavaScript, CSS, images, etc.) will be blocked until the current script is loaded.

    Both of these factors can slow down the perceived load time of your page.

    If your JavaScript does not alter the page or if it doesn't need to inspect the state of the page until it has loaded you can mark your script element with defer="defer" (supported by IE 6+ and Firefox 3.5+) which indicates that the evaluation of the script can happen "later". Moving your script elements to the bottom of the page effectively does the same thing - since your scripts appear at the end of the document they'll be evaluated after CSS, images, etc. are loaded and the HTML is rendered.

    0 讨论(0)
  • 2020-12-10 17:55

    I place all CSS in the HEAD to avoid excessive screen paintings and flashes of style.

    I place most JavaScript file requests at the bottom of the page so that the page can render quickly (HTML/CSS loading will block until script tags above them have been loaded and processed). Any code that needs to be executed after the library files have loaded are executed onDOMReady, which is all code except for library initialization. I pretty much followed Google's PageSpeed recommendations.

    I've been thinking about using LABjs as well to further decrease page load times, but this works well enough right now.

    0 讨论(0)
  • 2020-12-10 17:57

    Simply put, if your script have snippets that would start executing right away (outside all function(){} bodies) and that access DOM elements, it should go at the end of the page so that DOM would have been built and be ready to be accessed by the time the script starts executing.

    If you are accessing DOM only from function calls (like onload, onclick etc), you can put the script safely in the head section itself.

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