Search code examples
htmldomhtml-parsinghtml-parser

In which situations, the elements will be attached to Head element of DOM during HTML parsing?


Obviously, the contents/tags within <head></head> pair will be attached to Head element.

Is there any situation in which during the parsing of <body></body>, some elements would be attached to Head element, instead of Body element?


Solution

  • In the HTML5 parsing algorithm, no, except via injection by script. Some older browsers may move such elements though. For example see the thread that starts at http://lists.w3.org/Archives/Public/public-html/2007May/1066.html and in particular http://lists.w3.org/Archives/Public/public-html/2007May/1081.html

    However, there is the case of what happens to an element that's normally in head, appears either before the <head> tag, or after the </head> tag but before the body element is created, for instance http://software.hixie.ch/.... In this situation, the element will be moved inside the head element.

    Note that elements between the <head> and </head> tags can easily end up not inside the head element due to invalid mark up, for instance, http://software.hixie.ch/...