Skip to content Skip to sidebar Skip to footer

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 '

Solution 1:

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

Solution 2:

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.

Certain elements, like script, may appear in either the head or the body; 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. 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.

Post a Comment for "How Is Code *between* The End Of The Head Tag And Before The Body Tag Processed?"