views:

103

answers:

4

Is there ever an instance in which something should (or can?) be placed between the </body> and </html> tag? Or should they always be paired?

I was reading the HTML5 spec, and it got me thinking about the basic structure of a page.

Are there any cases where anything can/should be placed after the </html> tag? It used to be gospel that documents started with just <html>, but <!doctype...> changed that. Would there ever be a reason to change that for the end of a document?

A: 

No, not really. There's no reason to put anything after </html>. After all, you are defining an html document. The DocType was introduced because of inconsistencies in browser rendering.

As for after the body tag. I could one day see "foot" as a tag introduced to match the head at the top. The only thing that I can see it being used for would be javascript that is loaded after the document. Right now, that is just put right before the end of the body tag.

Atømix
+1  A: 

Some people like to put javascripts intended to run after the page has finished loading either at the very bottom of the body section or below the </body> tag. Really though there is no reason to do this if you are using jQuery, since the $(document).ready() function makes it moot.

No structural tags should be there however, and actually JS should be in the <head> section anyway.

smdrager
-1 for suggesting to put the JS inside the `<head>` element
Denis 'Alpheus' Čahuk
What? Why shouldn't you put javascript in the head? That's where it goes!
smdrager
un -1'ed. Don't fully agree (structurally/semantically, smdrager is right, but performance wise, Denis has a point), but some JavaScript (particularly asynchronous code) can go in the head without causing problems.
yc
+1 just because smdrager is completely right. Performance has nothing to do with being right or wrong.
zvonimir
“there is no reason to do this if you are using jQuery, since the $(document).ready() function makes it moot.” — Really? jQuery still has to load before you can use `$(document).ready()`, and if you link to jQuery in your `<head>` tag, the rest of the page won’t start rendering until jQuery’s been loaded. That’s fine if you’re happy with that, but if you do want the page to start rendering sooner, `$(document).ready()` doesn’t make the “put `<script>` tags immediately before `</body>`” suggestion moot.
Paul D. Waite
Oh — why should JS be in `<head>`?
Paul D. Waite
If you are using jQuery, and many many websites do these days, the vast majority of your scripts will be in the head. The only reason I ever put js below the body was to make sure it was triggered after the document loaded, but now with jQuery, there is no reason to do this--and doing can lead to confusion.jQuery can do almost anything from the head.The only reason it might not go there is if you have a partial element which needs some JS. In that case it might be more convent to have it by the element.
smdrager
Why would you ever 'have to' pust the javascript in the `head`? Talking about javascript, most people will use the `ready` event anyway, so there is no reason to put jQuery in the head, since the DOM will never be in any usable partial state yet and you're just blocking the fetch and the render needlessly. Further, if you have to make calls to jQuery between the start of the body and where you include JQ, just stack the callbacks up and call them after you've loaded jQuery. This can easily be achieved with a couple lines of clean code.
Denis 'Alpheus' Čahuk
A: 

Nothing can go after </html> nor between </body> and </html>.

On the other hand, bear in mind that HTML, HEAD and BODY are implied even if the tags don't exist, so an html document does not have to end with </html>.

Of course, browsers tend to try to guess what the author wanted rather than breaking on invalid markup, so yes, you can expect people putting something after </html>, because it does what they want.

zvonimir
+2  A: 

The <body> tag is optional since <frameset> can be used instead. Therefore, they are not always paired. White space can exist between the tags. Also, <body> can come before <head>.

Currently, anything after a closing </html> tag is generally considered inline text. Firefox and IE both render it.

The Document Type Definitions (DTDs) of each style hold the answers you seek.

Marcus Adams