Javascript 将 <script> 标签放在 </body> 标签之后是错误的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3037725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:02:30  来源:igfitidea点击:

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

javascripthtml

提问by DanC

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

将 script 标签放在 body ( </body>)的结束标签之后是多么错误。?

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

采纳答案by Andy E

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

它不会在or标签之外进行验证。它也不会有太大区别——除非你正在做可能在 body 元素完全加载之前破坏 IE 的DOM 操作——将它放在关闭.<body><head></body>

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

回答by Quentin

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

是的。在正文的结束标记之后只允许 html 元素的注释和结束标记。

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

浏览器可能会执行错误恢复,但您不应该依赖它。

回答by Vitalii Fedorenko

As Andysaid the document will be not valid, but nevertheless the script will still be interpreted. See the snippetfrom WebKit for example:

正如安迪所说,该文件将无效,但脚本仍将被解释。例如,请参阅来自 WebKit的片段

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;
    ...

回答by Bronx

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.

IE 不再允许这样做(我相信从版本 10 开始)并且将忽略此类脚本。FF 和 Chrome 仍然容忍它们,但有可能有一天他们会将其作为非标准丢弃。

回答by BG Bruno

Procedurally insert "element script" after "element body" is "parse error" by recommended process by W3C. In "Tree Construction" create error and run "tokenize again" to process that content. So it's like additional step. Only then can be runned "Script Execution" - see scheme process.

通过 W3C 推荐的过程,在“元素主体”之后按程序插入“元素脚本”是“解析错误” 。在“Tree Construction”中创建错误并运行“再次标记化”以处理该内容。所以这就像额外的步骤。只有这样才能运行“脚本执行”——参见方案过程

Anything else "parse error". Switch the "insertion mode" to "in body" and reprocess the token.

任何其他“解析错误”。将“插入模式”切换为“体内”并重新处理令牌。

Technically by browser it's internal process, how they mark and optimize it.

从技术上讲,浏览器是内部过程,它们如何标记和优化它。

I hope I helped somebody.

我希望我帮助了某人。

回答by Ad Charity

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.

基本上,这意味着一旦页面完成就会加载脚本,这在某些情况下可能很有用(即 DOM 操作)。但是,我强烈建议您使用相同的脚本并将其放在带有“defer”的 head 标记中,因为它会产生相同的效果。

<head>
    <script src="scripts/main.js" defer></script>
</head>

回答by Donald Porter

Google actually recommends this in regards to 'CSS Optimization'. They recommend in-lining critical above-fold styles and deferring the rest(css file).

Google 实际上在“CSS 优化”方面推荐了这一点。他们建议内联关键的上折叠样式并推迟其余样式(css 文件)。

Example:

例子:

<html>
  <head>
    <style>
      .blue{color:blue;}
    </style>
    </head>
  <body>
    <div class="blue">
      Hello, world!
    </div>
  </body>
</html>
<noscript><link rel="stylesheet" href="small.css"></noscript>


See: https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery

请参阅:https: //developers.google.com/speed/docs/insights/OptimizeCSSDelivery

回答by Taylor Satula

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.

是的。但是如果你在它之外添加代码很可能不会是世界末日,因为大多数浏览器都会修复它,但进入它仍然是一个不好的做法。