SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Single or multiple javascript blocks?

    Hey guys,

    I was just wondering if it's recommended to have all your javascript contained in a single unified block or if it's okay to have them spread out across the document? I find the second approach ensuring JS is located near code that is directly associated increases readability, but I'm worried it may have a negative effect on performance, however minor.

    Thanks,

    Bardi

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Placing all the JavaScript calls either at the end of the head or at the end of the body is the best way of making your page as maintainable as possible. It also makes it easier to comment them all out in order to easily test the without JavaScript version of the page in browsers that do not allow JavaScript to be easily turned on and off.
    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="^$">

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Having the script code spread out across the document strongly impacts page loading performance, because the web browser must stop all of its loading each and every time it comes to a script block, where it has to finish loading and executing that part of the script, before the browser is allowed to carry on loading parts of the page.

    See these Best Practices for Speeding Up Your Web Site
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys for the feedback - exactly the sort of clarification I was looking for!


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
  •