2

I'm learning how to appending DOM nodes with Javascript and have a little clarification question. Here's an example:

<html>
   <head>
   </head>
   <body>
   </body>
</html>

    document.childNodes[0]

So here, you would get the <head> node because it is the first child after the <html> tag. My question is can I always consider "document" to be the equivalent of the <html> tag or root node?

1
  • 1
    it depends on your context/environment - it will always be the document by default in the browser, but javascript can run in other environments
    – kinakuta
    Commented Feb 26, 2012 at 19:53

4 Answers 4

1

I'd recommend studying the W3C DOM spec as well: even if some parts of it won't mean much to you, it might save you a plenty of time and efforts later. )

That's what's said about document here:

The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

0

Root node for html is ... html, which can be get via document.documentElement

I'm not sure, it is supported in all modern browsers, though.

0

document.childNodes[0] will be the html tag in your example.

0

document.childNodes[0] is the <!DOCTYPE> node when I try it (though not sure if that's always the case).

document.documentElement is the <html> tag.

document.body is the <body> tag.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.