search

Home  >  Q&A  >  body text

Is it wrong to place <script> tag after </body> tag?

<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>
P粉654894952P粉654894952577 days ago603

reply all(2)I'll reply

  • P粉073857911

    P粉0738579112023-08-22 16:32:39

    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 defer attribute, we can place script inside and get this benefit while also having the browser download the HTML in parallel to Improve performance.

    reply
    0
  • P粉232409069

    P粉2324090692023-08-22 15:46:00

    It will not validate outside of the <body> or <head> tags. It also doesn't make much of a difference unless you're doing DOM manipulation before the element is fully loaded which might break IE - put it before closing the .

    <html>
      ....
      <body>
         ....
         <script type="text/javascript" src="theJs.js"></script>
      </body>
    </html>

    reply
    0
  • Cancelreply