简体   繁体   English

代码*如何在* HEAD标记的结尾和BODY标记处理之间?

[英]How is code *between* the end of the HEAD tag and before the BODY tag processed?

I have read that it is bad to do this, and outside the spec, but I have an error that is somewhat intermittent, and may be related. 我已经读到这样做是不好的 ,并且在规范之外,但我有一个有点间歇性的错误,可能是相关的。 Just want to know if the general consensus is "burn that code with napalm" or "it works, under these limited conditions". 只是想知道普遍的共识是“用凝固汽油弹烧掉那个代码”还是“它在这些有限的条件下工作”。

I have found, in a very complex code base we maintain, <script> tags, both loading from external sources (javascript and jquery), and inline scripts, after the </head> and before the <body> . 我发现,在一个非常复杂的代码库中,我们维护了<script>标签,它们都来自外部源(javascript和jquery),内联脚本,在</head><body>之前。 Most of it is code that needs to load after the form is fully loaded. 大部分是在表单完全加载后需要加载的代码。

Before I start making changes to incorporate the code into the bottom of the Head tag, I'd like to understand what is happening right now to clients that are running this code (the goal is to not break the current user experience). 在我开始做改革,以便把代码到head标签的底部,我想明白发生了什么,现在到运行该代码的客户(我们的目标是不会打破当前的用户体验)。 Would these scripts be loaded before the page is rendered, or after? 这些脚本是在呈现页面之前加载还是之后? What is the actual state of this no-man's-land code that shouldn't be there in the first place? 这个无人区代码的实际状态首先不应该存在于什么状态?

Thanks! 谢谢!

Fundamentally, it's invalid markup and a parsing error that the browser will have to recover from. 从根本上说,它是无效的标记和浏览器必须从中恢复的解析错误。 So I would test this in your target browsers to see what they do. 所以我会在你的目标浏览器中测试它,看看它们做了什么。

In theory: After the closing </head> tag and before the opening <body> tag, the parser is in " 'after head' insertion mode ". 理论上:在结束</head>标记之后和开始<body>标记之前,解析器处于“ 'head'插入模式之后 ”。 I've listed the rules for that below, but thanks to your comment , I think the relevant one is: 我已经列出了下面的规则,但是由于你的评论 ,我认为相关的是:

  • A start tag whose tag name is one of: "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title" 标签名称为以下之一的开始标记:“base”,“basefont”,“bgsound”,“link”,“meta”,“noframes”,“script”,“style”,“template”,“title”
    • Parse error. 解析错误。
    • Push the node pointed to by the head element pointer onto the stack of open elements. 将头元素指针指向的节点推到打开元素的堆栈上。
    • Process the token using the rules for the "in head" insertion mode. 使用“in head”插入模式的规则处理令牌。
    • Remove the node pointed to by the head element pointer from the stack of open elements. 从开放元素堆栈中删除头元素指针指向的节点。 (It might not be the current node at this point.) (此时可能不是当前节点。)
    • The head element pointer cannot be null at this point. 此时头元素指针不能为空。

Which tells us that: 这告诉我们:

  1. It's a parsing error, see my opening paragraph. 这是一个解析错误,请参阅我的开头段落。 :-) :-)
  2. The spec seems to be telling the browser vendors to treat the script as though it were at the end of the head . 该规范似乎是告诉浏览器厂商对待剧本,就好像是在结束head

The full rules: 完整规则:

When the user agent is to apply the rules for the "after head" insertion mode, the user agent must handle the token as follows: 当用户代理要应用“后头”插入模式的规则时,用户代理必须按如下方式处理令牌:

  • A character token that is one of U+0009 CHARACTER TABULATION, "LF" (U+000A), "FF" (U+000C), "CR" (U+000D), or U+0020 SPACE 字符标记是U + 0009字符制表,“LF”(U + 000A),“FF”(U + 000C),“CR”(U + 000D)或U + 0020 SPACE之一

    • Insert the character. 插入角色。
  • A comment token 评论标记

    • Insert a comment. 插入评论。
  • A DOCTYPE token DOCTYPE令牌

    • Parse error. 解析错误。 Ignore the token. 忽略令牌。
  • A start tag whose tag name is "html" 标签名称为“html”的开始标记

    • Process the token using the rules for the "in body" insertion mode. 使用“in body”插入模式的规则处理令牌。
  • A start tag whose tag name is "body" 标签名称为“body”的开始标记

    • Insert an HTML element for the token. 为令牌插入HTML元素。
    • Set the frameset-ok flag to "not ok". 将frameset-ok标志设置为“not ok”。
    • Switch the insertion mode to "in body". 将插入模式切换为“in body”。
  • A start tag whose tag name is "frameset" 标签名称为“frameset”的开始标记

    • Insert an HTML element for the token. 为令牌插入HTML元素。
    • Switch the insertion mode to "in frameset". 将插入模式切换为“in frameset”。
  • A start tag whose tag name is one of: "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title" 标签名称为以下之一的开始标记:“base”,“basefont”,“bgsound”,“link”,“meta”,“noframes”,“script”,“style”,“template”,“title”

    • Parse error. 解析错误。
    • Push the node pointed to by the head element pointer onto the stack of open elements. 将头元素指针指向的节点推到打开元素的堆栈上。
    • Process the token using the rules for the "in head" insertion mode. 使用“in head”插入模式的规则处理令牌。
    • Remove the node pointed to by the head element pointer from the stack of open elements. 从开放元素堆栈中删除头元素指针指向的节点。 (It might not be the current node at this point.) (此时可能不是当前节点。)
    • The head element pointer cannot be null at this point. 此时头元素指针不能为空。
  • An end tag whose tag name is "template" 标记名称为“模板”的结束标记

    • Process the token using the rules for the "in head" insertion mode. 使用“in head”插入模式的规则处理令牌。
  • An end tag whose tag name is one of: "body", "html", "br" 标记名称为以下之一的结束标记:“body”,“html”,“br”

    • Act as described in the "anything else" entry below. 按照下面“其他任何”条目中的描述行事。
  • A start tag whose tag name is "head" 标签名称为“head”的开始标记

  • Any other end tag 任何其他结束标记

    • Parse error. 解析错误。 Ignore the token. 忽略令牌。
  • Anything else 还要别的吗

    • Insert an HTML element for a "body" start tag token with no attributes. 为没有属性的“body”开始标记标记插入HTML元素。
    • Switch the insertion mode to "in body". 将插入模式切换为“in body”。
    • Reprocess the current token. 重新处理当前令牌。

Content appearing between the </head> end tag and <body> start tag gets inserted just past the <body> start tag, unless it's an element that does not belong in the body, in which case it will be inserted at the end of the head. </head>结束标记和<body>开始标记之间出现的内容会直接插入<body>开始标记,除非它是一个不属于正文的元素,在这种情况下它将插入到结尾处头部。

Certain elements, like script , may appear in either the head or the body; 某些元素,如script ,可能出现在头部或身体中; script elements in that location will be inserted at the end of the head, unless the browser is already in the "in body" insertion mode, in which case they'll be placed in the body. 该位置的脚本元素将插入头部的末尾, 除非浏览器已经处于“体内”插入模式,在这种情况下,它们将被放置在正文中。

See sections 8.2.5.4.4 The "in head" insertion mode , 8.2.5.4.6 The "after head" insertion mode and 8.2.5.4.7 The "in body" insertion mode of W3C HTML for further details. 参见8.2.5.4.4“头部”插入模式8.2.5.4.6“后头”插入模式8.2.5.4.7 W3C HTML 的“体内”插入模式 ,以获取更多详细信息。 Note that implementations are not guaranteed to follow the spec, either owing to historical reasons, bugs, or deliberate decision to violate the spec, and this behavior should not be relied on. 请注意,由于历史原因,错误或故意违反规范的决定,不保证实现遵循规范,并且不应依赖此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在页面的顶部将css文件包含到php中,在body标记结束之前包含js文件? - How do I include a css file into php in the head section of the page and a js file before the end of body tag? 在body标签结尾之前添加text / html - Add text/html before end of body tag 如何处理标签中的正文加载事件 - How to handle body onload event in <head> tag 如何删除html标签和head标签之间的iframe标签? - How to remove iframe tag between html tag and head tag? 在正文标签末尾添加脚本标签(无效) - Adding script tag before the end of body tag(Not Working) 当javascript必须包含在 <head> 标签,而不是之前 </body> ? - When javascript must be included in the <head> tag and not before </body>? 将javascript代码片段附加到body标记的末尾 - Appending javascript code piece to the end of the body tag 是不是写的比较快<script defer> just before the </ head> tag than before the </ body> tag until the processing is completed? - Is it faster to write <script defer> just before the </ head> tag than before the </ body> tag until the processing is completed? 在之前包含Javascript的优缺点是什么? </head> 标签与 </body> 标签? - What are the pros and cons of including Javascript right before the </head> tag vs the </body> tag? LABjs:如何将脚本附加到body标记的末尾 - LABjs : How to append the scripts to the end of the body tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM