Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

document or document.body

0 views
Skip to first unread message

Martin Rinehart

unread,
Dec 6, 2008, 7:49:57 AM12/6/08
to
I've been attaching tables and other things directly to document. It
seems to work. Should I be using document.body? What is/are the
difference(s)?

Thomas 'PointedEars' Lahn

unread,
Dec 6, 2008, 8:07:44 AM12/6/08
to
Martin Rinehart wrote:
> I've been attaching tables and other things directly to document.

"Attaching" -- how?

> It seems to work.

Testing things positive to "work" in a handful of newest UAs without
understanding the mechanisms behind them is the first blunder.

> Should I be using document.body

Yes if you are talking about Element::appendChild() or Node::insertBefore()
and objects representing elements that belong within the `body' element.

> What is/are the difference(s)?

It makes sense in the former context.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

Martin Rinehart

unread,
Dec 6, 2008, 9:37:54 AM12/6/08
to
Thomas 'PointedEars' Lahn wrote:

> > What is/are the difference(s)?
>
> It makes sense in the former context.

I can't make sense of "It makes sense." What is the difference between
document.appendChild() and document.body.appendChild()? Both seem to
display the child.

Thomas 'PointedEars' Lahn

unread,
Dec 6, 2008, 10:12:32 AM12/6/08
to
Martin Rinehart wrote:
> Thomas 'PointedEars' Lahn wrote:
>>> What is/are the difference(s)?
>> It makes sense in the former context.
>
> I can't make sense of "It makes sense."

Tough luck.

> What is the difference between document.appendChild() and
> document.body.appendChild()?

The former is incorrect, the latter is not.

> Both seem to display the child.

Yes, that only seems to be so.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Martin Honnen

unread,
Dec 6, 2008, 10:25:26 AM12/6/08
to
Martin Rinehart wrote:

> What is the difference between
> document.appendChild() and document.body.appendChild()? Both seem to
> display the child.

Call appendChild on the node you want to be the new parent node of the
child you want to insert. If you built an HTML document from scratch
then you would do e.g.
var html = document.createElement('html');
document.appendChild(html);

I wouldn't rely on the browser guessing that
document.appendChild(document.createElement('table')) is meant to insert
the table element into the body. It certainly does not work in IE or
Firefox. IE 7 does not raise an error but does not insert the element.
Firefox 3.0 does raise an error "Error: uncaught exception:
[Exception... "Node cannot be inserted at the specified point in the
hierarchy"". Safari 3.2 raises an error too "HIERARCHY_REQUEST_ERR: DOM
Exception 3".


--

Martin Honnen
http://JavaScript.FAQTs.com/

Gregor Kofler

unread,
Dec 6, 2008, 11:12:51 AM12/6/08
to
Martin Rinehart meinte:

> I've been attaching tables and other things directly to document. It
> seems to work.

With which browser? The "normal" ones won't accept that.

Gregor

Martin Rinehart

unread,
Dec 7, 2008, 7:18:28 AM12/7/08
to
PE, I already know your manners are atrocious. That's not news.

What would be helpful is to know why document.body is correct and
document is not. Even more helpful would be an explanation of the
consequences of using the wrong one; the benefits of using the right
one.

Martin Rinehart

unread,
Dec 7, 2008, 7:26:29 AM12/7/08
to

Martin Honnen wrote:
> I wouldn't rely on the browser guessing that
> document.appendChild(document.createElement('table')) is meant to insert
> the table element into the body. It certainly does not work in IE or
> Firefox. IE 7 does not raise an error but does not insert the element.
> Firefox 3.0 does raise an error "Error: uncaught exception:
> [Exception... "Node cannot be inserted at the specified point in the
> hierarchy"". Safari 3.2 raises an error too "HIERARCHY_REQUEST_ERR: DOM
> Exception 3".

Thanks, Martin. I'll switch to Windows and turn on Fox's error
reporting.

But now I'm even more confused. Is there a difference between
document.html and document.body? And why isn't it document.html.body?
Too DOM many irregularities.

Martin Rinehart

unread,
Dec 7, 2008, 7:28:30 AM12/7/08
to
Gregor Kofler wrote:

> With which browser? The "normal" ones won't accept that.

Opera.

Thomas 'PointedEars' Lahn

unread,
Dec 7, 2008, 7:34:06 AM12/7/08
to
Martin Rinehart wrote:
> But now I'm even more confused. Is there a difference between
> document.html and document.body? And why isn't it document.html.body?
> Too DOM many irregularities.

Which DOM provides document.html in the first place?

Lasse Reichstein Nielsen

unread,
Dec 7, 2008, 7:39:42 AM12/7/08
to
Martin Rinehart <MartinR...@gmail.com> writes:

> PE, I already know your manners are atrocious. That's not news.

Awww, come on! If you tell people they are wrong, and at the same time
tell them what is right, they might actually feel smarter afterwards
instead of feeling inferior. How would that make you feel better then?

> What would be helpful is to know why document.body is correct and
> document is not. Even more helpful would be an explanation of the
> consequences of using the wrong one; the benefits of using the right
> one.

The problem with appending HTML elements to "document", is that the
resulting DOM structure is not a valid HTML document.

In HTML, elements should go into either head or body, with visible
content in the body. That is what you achieve by adding children to
the body element. (And yes, script elements not inside either head
or body is also invalid HTML, even though it's pretty common.)

If you add elements as children of the document element, i.e., the
equvialent of adding them directly inside the <html> element in
HTML syntax, some browsers will likely do error-correction and
"make it work" anyway. However, there is no standard specifying
how it should work. Will styles put on the body element be
inherited? Will body.onclick be triggered by clicking on your
element? It's likely to blow up in your face at the most unfortunate
moment.

/L ';)'
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

Lasse Reichstein Nielsen

unread,
Dec 7, 2008, 7:44:36 AM12/7/08
to
Lasse Reichstein Nielsen <lrn.u...@gmail.com> writes:

> If you add elements as children of the document element, i.e., the
> equvialent of adding them directly inside the <html> element in
> HTML syntax, some browsers will likely do error-correction and
> "make it work" anyway.

I forgot that the original distinction was not between
document.documentElement and document.body, but between document and
document.body. Adding directly to document (if it is at all possible)
is even less specified than adding them to document.documentElement
(the node corresponding to the <html> element in HTML syntax).
The document object does not correspond to anything in the HTML syntax,
but is merely a container for the actual HTML elements' DOM nodes.

/L

Thomas 'PointedEars' Lahn

unread,
Dec 7, 2008, 7:46:25 AM12/7/08
to
Martin Rinehart wrote:
> PE, I already know your manners are atrocious. That's not news.

Likewise, luser. <http://jibbering.com/faq/#posting>

> What would be helpful is to know why document.body is correct and
> document is not.

`document' refers to an object that implements the (HTML)Document interface;
`document.body' refers to an object that implements the HTMLBodyElement
interface. To get it running, you put tires on the wheels of the car, not
merely anywhere on the car.

> Even more helpful would be an explanation of the
> consequences of using the wrong one; the benefits of using the right
> one.

Do I need to spell it for you? Using the wrong one does not work, using the
right one does work.

Martin Honnen

unread,
Dec 7, 2008, 8:06:31 AM12/7/08
to
Martin Rinehart wrote:

> But now I'm even more confused. Is there a difference between
> document.html and document.body? And why isn't it document.html.body?
> Too DOM many irregularities.

I don't know of any document.html property.
There is a document.documentElement property however.
And of course there is a difference between document.documentElement and
document.body, first of all document.documentElement exists in the Core
DOM while document.body is part of the HTML DOM only. document.body is a
shortcut in the HTML DOM, much like document.images or document.forms
(although these refer to collections while document.body refers to the
sole body element).

0 new messages