4

Possible Duplicate:
Is it wrong to place the <script> tag after the </body> tag?

//scripts
</body>
</html>

or

</body>
//scripts
</html>

I know it's kind of a silly question but is there any difference between these two methods. I've read in books to do just before the </body> tag but in practice I've seen people do it just before the </html> tag. My instincts and current findings say that it shouldn't matter I don't think it would but being as I'm newer to web development I always second guess my work cause there could be something I don't know about.

Community
  • 1
  • 1
Cody
  • 578
  • 3
  • 13
  • 10
    I don't believe the ` – Shmiddty Dec 19 '12 at 17:17
  • 1
    I don't think tags are allowed outside the head or body tag. – Christophe Dec 19 '12 at 17:17
  • 3
    It's not valid HTML, – Alex K. Dec 19 '12 at 17:17
  • 1
    This seems like the type of question that could be answered just by looking at the [W3C HTML spec](http://www.w3.org/TR/2011/WD-html5-20110525/scripting-1.html#the-script-element). Just sayin'... – Brian Driscoll Dec 19 '12 at 17:18
  • @BrianDriscoll Wait? This stuff is standardized? `:P` – Šime Vidas Dec 19 '12 at 17:20
  • If you are reffering to why some scripts are loaded **before the closing `

    `** instead of **inside the `

    `** then these might help: - [Putting jQuery/javascript source pages before end of body tag](http://stackoverflow.com/questions/9343392/putting-jquery-javascript-source-pages-before-end-of-body-tag) and [Move jQuery to the end of body tag?](http://stackoverflow.com/questions/1220956/move-jquery-to-the-end-of-body-tag)

    – Adam Tomat Dec 19 '12 at 17:29

2 Answers2

5

The comments are correct: <script> outside the head or body are not valid HTML.

Here is the relevant spec

Content Model

A head element followed by a body element.
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
0

I'm not an expert, but in my experience, as long as your JS is in the header or body tag, it doesn't make a large difference where you place your JS.

I and my coworkers always place our JS in the header tag (for readability and consistency). But I've read (here and here) that to get the best performance you should place you JS just before your closing BODY tag. Again, in my experience I haven't noticed a major performance difference.

Community
  • 1
  • 1
barbajoe
  • 547
  • 5
  • 15