Problem
Is it really so bad to put the script tag after the body’s closing tag (/body>)?
<html>
....
<body>
....
</body>
<script type="text/javascript" src="theJs.js"></script>
</html>
Asked by DanC
Solution #1
Outside of the body> and head> tags, it will not validate. Putting it immediately before the closing /body> won’t make much impact, either, unless you’re executing DOM changes that could break IE before the body element is entirely loaded.
<html>
....
<body>
....
<script type="text/javascript" src="theJs.js"></script>
</body>
</html>
Answered by Andy E
Solution #2
Yes. After the end tag for the body, only comments and the end tag for the html element are allowed.
Although some browsers offer error recovery, you should never rely on it.
Answered by Quentin
Solution #3
The document will not be valid, but the script will still be interpreted, as Andy stated. Consider the following WebKit snippet:
void HTMLParser::processCloseTag(Token* t)
{
// Support for really broken HTML.
// we never close the body tag, since some stupid web pages close it before
// the actual end of the doc.
// let's rely on the end() call to close things.
if (t->tagName == htmlTag || t->tagName == bodyTag
|| t->tagName == commentAtom)
return;
...
Answered by Vitalii Fedorenko
Solution #4
Internet Explorer no longer allows this (since version 10, I believe) and will discard scripts of this nature.
Firefox and Chrome still tolerate them, but there are chances that some day they will drop this as non-standard.
Answered by Bronx
Solution #5
The suggested W3C method considers putting a “element script” after a “element body” to be a “parse error.” Create an error in “Tree Construction” and then run “tokenize again” to process the material. As a result, it’s like an extra step. The “Script Execution” – see the scheme process – can then be run.
Technically, how the browser marks and optimizes it is an internal operation.
Answered by BGBRUNO
Post is based on https://stackoverflow.com/questions/3037725/is-it-wrong-to-place-the-script-tag-after-the-body-tag