Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    document.documentElement vs document.body

    To my knowledge, for IE, document.documentElement works for standard mode, while the document.body works for the quirk mode. For instance:


    if (document.documentElement){

    alert(document.documentElement.clientHeight);
    //This does not works. It displays a 0.
    //Instead, the following works:

    alert(document.body.clientHeight);

    }else if (document.body){

    alert(document.body.clientHeight);

    }

    Why does not the document.documentElement.clientHeight work for the standard mode?

    And why document.body.clientHeight works for both modes?

    I believe they should work only for either mode.



    Thanks


    Scott

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    It has to do with which element is considered the element that establish both the canvas and the viewport. In standards mode that is the HTML element, in quirks mode that is the BODY element. However, the position APIs need to be able to work on all elements from the one that establishes the canvas and the viewport, respectively.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Why does not the document.documentElement.clientHeight work for the standard mode?And why document.body.clientHeight works for both modes?
    because clientHeight needs a parent element to contain its dimensions.

    document.documentElement is the html element, and has no parent.

    document.body has a parent element, but the offsets (margins) may be different in strict and standards mode.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It is a matter of how the browsers, according to the type/version and standard(strict)/quirks mode (with/without a Doctype) make the difference between window's viewport , body, document and documentElement. The behaviors are peculiar and hardly to be set in a general rule.

    According to the type and mode, body and/or documentElement might be the block element (the element which bears the size) of the window's viewport, of the document or even of both

    Sounds messy, I know, but don't ask me why, ask the people who created those browsers.

    For instance, several examples

    1. IE 6-8 strict, Mozilla strict and Opera 9.5+ strict
    document.body.clientHeight = document's height
    document.documentElement.clientHeight = window's viewport height

    2. IE 5 and IE 6-8 quirks
    document.body.clientHeight = window's viewport height
    document.documentElement.clientHeight = 0

    3. Opera 7-9.2 and Opera 9.5 quirks
    document.body.clientHeight = window's viewport height
    document.documentElement.clientHeight = document's height

    4.Safari
    document.body.clientHeight = document's height
    document.documentElement.clientHeight = document's height

    Other browsers behave alike or between the above.

    ----
    Additional note: To simplify the explanation I said that strict mode is the same with standard mode and it means a document with a Doctype. Well, it is half true. To be strict, documents should rather bear a strict Doctype. And even so, browsers (even those which claimed to be full DOM compatible, like Moz and Opera) are in, so far, almost standard mode... But this is another topic...
    Last edited by Kor; 04-26-2008 at 07:55 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Now I understand better. Thanks.

    As for the Doctype to trigger the standard/quirk mode, what is the format of the Doctype?


    Scott

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •