document or document.body

  • Thread starter Martin Rinehart
  • Start date
M

Martin Rinehart

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)?
 
T

Thomas 'PointedEars' Lahn

Martin said:
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
 
M

Martin Rinehart

Thomas said:
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.
 
M

Martin Honnen

Martin said:
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".
 
G

Gregor Kofler

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
 
M

Martin Rinehart

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.
 
M

Martin Rinehart

Martin said:
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.
 
T

Thomas 'PointedEars' Lahn

Martin said:
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
 
L

Lasse Reichstein Nielsen

Martin Rinehart said:
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 ';)'
 
L

Lasse Reichstein Nielsen

Lasse Reichstein Nielsen said:
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
 
T

Thomas 'PointedEars' Lahn

Martin said:
PE, I already know your manners are atrocious. That's not news.

Likewise said:
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
 
M

Martin Honnen

Martin said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top