JS placement: Beginning or at the end of the file?

Discussion in 'JavaScript' started by enchance, May 15, 2008.

  1. #1
    Which is the safer bet, to place your JS (including external JS) within the <head></head> tags or at the very end of your <body></body> tag? Or better yet, is it more efficient to simply use the code below:
    
    
    <head>
    <script type="text/javascript">
    //Method 1:
    document.body.onload = function(){
         //execute all js functions
    }
    
    //Method 2: Is this even correct?
    document.onload = function(){
         //execute all js functions
    }
    </script>
    </head>
    <body>
    </body>
    
    Code (markup):
    Which do you prefer to use?
     
    enchance, May 15, 2008 IP
  2. chenyaniv

    chenyaniv Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i always put my js code in the <Head></head> part, exept the part thats importent to the website flow.
     
    chenyaniv, May 15, 2008 IP
  3. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #3
    I always put my code in the head tags as well.
     
    crath, May 15, 2008 IP