[whatwg] document.head

Michael A. Puls II shadow2531 at gmail.com
Tue Oct 6 07:43:54 PDT 2009


On Tue, 06 Oct 2009 10:14:13 -0400, Hallvord R M Steen  
<hallvors at gmail.com> wrote:

>> You might think so. However, as Michael stated above Opera used to do
>> this, and it broke a number of websites that expected
>> documentElement.firstChild to be <HEAD> no matter what the actual
>> markup looked like. So we had to implement a somewhat magic firstChild
>> on documentElement
>
> ..though less magic than I remembered - it only skips text nodes, not
> comments.. And perhaps what happens is that the text node is put
> inside body instead? I'd have to do some more testing to tell what
> browsers actually do..

Here's an example to play with:

document.documentElement.firstChild results:

<!DOCTYPE html>
<html><!--Not head --><head>
<title></title>
<script>
     window.onload = function() {
         var de = document.documentElement;
         var fc = de.firstChild;
         alert(fc);
         de.insertBefore(document.createTextNode("test"), fc);
         alert(de.firstChild);
     };
</script>
</head>

Safari, Opera, and Firefox with HTML5 parser enabled:
alert1: comment node
alert2: text node

IE8 and Firefox:
alert1: head element
alert2: text node

document.head definitely solves that mess.

-- 
Michael


More information about the whatwg mailing list