Is it wrong to place <script> tag after </body> tag?
P粉654894952
2023-08-21 23:56:17
<p>How wrong is it to put a script tag after the closing body tag (<code></body></code>)? </p>
<pre class="brush:php;toolbar:false;"><html>
....
<body>
....
</body>
<script type="text/javascript" src="theJs.js"></script>
</html></pre>
After the body closing tag, only comments and closing tags of html elements are allowed to be added.
You can confirm via Specification or Validator.
Browsers may perform error recovery, and the HTML specification even describes how to recover in this case, but you should never rely on this.
With the advent of the
and get this benefit while also having the browser download the HTML in parallel to Improve performance.defer
attribute, we can placescript
insideIt will not validate outside of the
element is fully loaded which might break IE - put it before closing the .<body>
or<head>
tags. It also doesn't make much of a difference unless you're doing DOM manipulation before the