Script tags in header or end of body tag?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TerraFree
    New Member
    • Oct 2020
    • 1

    Script tags in header or end of body tag?

    Where should i put my javascript file calls?

    Inside the head tag or at the end of the body tag?

    Are there benefits for choosing one over the other?
  • SwissProgrammer
    New Member
    • Jun 2020
    • 220

    #2
    Javascript file calls should be placed only as close to the first part of your HTML as is needed for speed of loading the page and for speed of the user getting locally useful information from the page.

    Example: If your page has a lot of text that is of far more importance to the user than the effects of your Javascript, then place the Javascript further down the page to allow the text to show up quicker.

    Example: If your user might have intermittent or unstable or slow speed downloads, and if your Javascript is simply to make trailing sparkles behind the mouse, and if your user might have to wait a long time to get the actual page to load, then put that Javascript near or at the bottom of the HTML of the page.

    Example: If your user is looking for a Virtual Reality rendering which has user interaction capacity like WRL has, and if the text and other parts of the page are less important, then put the Javascript that creates the VR near to the top of the page.

    You have to determine the placement of the Javascript based upon the needs of the user of the page (within reason).

    There are a lot of pages that are full of what the author thinks is impressive, but that irritate the user.

    Think about each application and do what is needed.

    .

    Comment

    • dev7060
      Recognized Expert Contributor
      • Mar 2017
      • 656

      #3
      Where should i put my javascript file calls?

      Inside the head tag or at the end of the body tag?

      Are there benefits for choosing one over the other?
      Depends. If the script is interacting with the DOM, you have to place it before the closing of the </body>, otherwise, all DOM objects would hold null.

      Comment

      • AjayGohil
        New Member
        • Apr 2019
        • 83

        #4
        Hey

        The best practice is to put the <script> tags at the end of the document body so they don't block rendering of the page. But If there are active components on your page which are using external scripts,
        then their dependency (js files) should come before that (ideally in head tag).

        Comment

        Working...