document or document.body

Discussion in 'Javascript' started by Martin Rinehart, Dec 6, 2008.

  1. 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)?
    Martin Rinehart, Dec 6, 2008
    #1
    1. Advertising

  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>
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #2
    1. Advertising

  3. 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.
    Martin Rinehart, Dec 6, 2008
    #3
  4. 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
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #4
  5. 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/
    Martin Honnen, Dec 6, 2008
    #5
  6. 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
    Gregor Kofler, Dec 6, 2008
    #6
  7. 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, Dec 7, 2008
    #7
  8. 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, Dec 7, 2008
    #8
  9. Gregor Kofler wrote:

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


    Opera.
    Martin Rinehart, Dec 7, 2008
    #9
  10. 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
    Thomas 'PointedEars' Lahn, Dec 7, 2008
    #10
  11. Martin Rinehart <> 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, Dec 7, 2008
    #11
  12. Lasse Reichstein Nielsen <> 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'
    Lasse Reichstein Nielsen, Dec 7, 2008
    #12
  13. 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
    Thomas 'PointedEars' Lahn, Dec 7, 2008
    #13
  14. 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/
    Martin Honnen, Dec 7, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oney
    Replies:
    0
    Views:
    390
  2. =?Utf-8?B?RWFybCBUZWlncm9i?=

    document.body.onclick not working in script

    =?Utf-8?B?RWFybCBUZWlncm9i?=, Aug 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    32,131
    =?Utf-8?B?RWFybCBUZWlncm9i?=
    Aug 23, 2004
  3. =?Utf-8?B?U3RlcGhlbiBSLiBHaWJzb24=?=

    Streaming only the body of an MSWord document

    =?Utf-8?B?U3RlcGhlbiBSLiBHaWJzb24=?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    358
    John Timney \( MVP \)
    Jan 20, 2006
  4. London Boy
    Replies:
    2
    Views:
    313
    Keith Bowes
    Jan 12, 2004
  5. Replies:
    5
    Views:
    196
    A. Sinan Unur
    Sep 8, 2005
Loading...

Share This Page