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

后端 未结 2 431
慢半拍i
慢半拍i 2021-01-21 07:46

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 i

2条回答
  •  无人共我
    2021-01-21 08:27

    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 tag and before the opening tag, the parser is in "'after head' insertion mode". 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"
      • 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.
      • 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.

    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

      • Insert the character.
    • A comment token

      • Insert a comment.
    • A DOCTYPE token

      • Parse error. Ignore the token.
    • A start tag whose tag name is "html"

      • Process the token using the rules for the "in body" insertion mode.
    • A start tag whose tag name is "body"

      • Insert an HTML element for the token.
      • Set the frameset-ok flag to "not ok".
      • Switch the insertion mode to "in body".
    • A start tag whose tag name is "frameset"

      • Insert an HTML element for the token.
      • Switch the insertion mode to "in frameset".
    • A start tag whose tag name is one of: "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.
      • 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.
    • An end tag whose tag name is one of: "body", "html", "br"

      • Act as described in the "anything else" entry below.
    • A start tag whose tag name is "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.
      • Switch the insertion mode to "in body".
      • Reprocess the current token.

提交回复
热议问题