Is it wrong to place the [removed] tag after the </body> tag?

前端 未结 8 992
长情又很酷
长情又很酷 2020-11-22 06:12

How wrong is it to place the script tag after the closing tag of the body (). ?


  ....
  
     ....
  <         


        
相关标签:
8条回答
  • 2020-11-22 06:18

    IE doesn't allow this anymore (since Version 10, I believe) and will ignore such scripts. FF and Chrome still tolerate them, but there are chances that some day they will drop this as non-standard.

    0 讨论(0)
  • 2020-11-22 06:19

    Yes. Only comments and the end tag for the html element are allowed after the end tag for the body.

    Browsers may perform error recovery, but you should never depend on that.

    0 讨论(0)
  • 2020-11-22 06:24

    Yes. But if you do add the code outside it most likely will not be the end of the world since most browsers will fix it, but it is still a bad practice to get into.

    0 讨论(0)
  • 2020-11-22 06:25

    As Andy said the document will be not valid, but nevertheless the script will still be interpreted. See the snippet from WebKit for example:

    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;
        ...
    
    0 讨论(0)
  • 2020-11-22 06:27

    Modern browsers will take script tags in the body like so:

    <body>
        <script src="scripts/main.js"></script>
    </body>
    

    Basically, it means that the script will be loaded once the page has finished, which may be useful in certain cases (namely DOM manipulation). However, I highly recommend you take the same script and put it in the head tag with "defer", as it will give the same effect.

    <head>
        <script src="scripts/main.js" defer></script>
    </head>
    
    0 讨论(0)
  • 2020-11-22 06:31

    It won't validate outside of the <body> or <head> tags. It also won't make much difference — unless you're doing DOM manipulations that could break IE before the body element is fully loaded — to putting it just before the closing </body>.

    <html>
      ....
      <body>
         ....
         <script type="text/javascript" src="theJs.js"></script>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题