SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advised to put JS at bottom of page (not header) to increase loading times

    My firefox plugin called yslow that gives advice on speeding up loading times says:
    JavaScript scripts block parallel downloads; that is, when a script is downloading, the browser will not start any other downloads. To help the page load faster, move scripts to the bottom of the page if they are deferrable.

    I always thought that js should be in the header. What do you think? Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    When possible placing JavaScript before the closing body tag is best practice considering the content of the page will not be dragged down by any JavaScript that needs to load. The JavaScript can load after all the content is available. So yes, it is considered proper practice to place it before the closing body tag – when possible. Moe important than that though is to serve it all up in as few requests as possible ie. try to avoid excessive script tags unless absolutely necessary – most likely the case of vendor or third party code.
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks oddz for clearing that up. Very interesting I always put it in the head- now I know better!

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Most JavaScript single threads while other files can download 8 files at a time. So scripts in the head will delay 8 threads woth of file downloads while delaying the script to the end of the body means that only that one file (which probably can't run until the rest of the HTML has loaded anyway) is delayed in loading. So the rest of the page loads a lot faster. With the script at the bottom you can simplify the script as well because you can get rid of all the tests for whether the page has loaded yet. Effectively this means that the script can start running a lot sooner as well.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •