f



document or document.body

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)?
0
12/6/2008 12:49:57 PM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

13 Replies
743 Views

Similar Articles

[PageSpeed] 2

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>
0
Thomas
12/6/2008 1:07:44 PM
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.
0
Martin
12/6/2008 2:37:54 PM
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
0
Thomas
12/6/2008 3:12:32 PM
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/
0
Martin
12/6/2008 3:25:26 PM
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
0
Gregor
12/6/2008 4:12:51 PM
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.
0
Martin
12/7/2008 12:18:28 PM

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.
0
Martin
12/7/2008 12:26:29 PM
Gregor Kofler wrote:

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

Opera.
0
Martin
12/7/2008 12:28:30 PM
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?


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
0
Thomas
12/7/2008 12:34:06 PM
Martin Rinehart <MartinRinehart@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'
  
0
Lasse
12/7/2008 12:39:42 PM
Lasse Reichstein Nielsen <lrn.unread@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
-- 
Lasse Reichstein Holst Nielsen
 'Javascript frameworks is a disruptive technology'
  
0
Lasse
12/7/2008 12:44:36 PM
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.


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
0
Thomas
12/7/2008 12:46:25 PM
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).

-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/
0
Martin
12/7/2008 1:06:31 PM
Reply: