Cause Analysis of replacing document. body with document.doc umentElement

Source: Internet
Author: User

When the page content of IE6 exceeds the window size, scrollWidth, clientWidth, and offsetWidth are all interpreted as the actual width of the content.
The document. the body property does not return the expected results for us. For example, we use document. body. clientHeight originally wanted to get the "visible area height of the page", but actually returned the "actual content height of the page ".
What should we do? After adding the document DTD type, you can no longer get the attributes such as "visible area height" and "actual content width?
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> documentElement </title>
<Style type = "text/css">
Body {margin: 0; padding: 0; font: 12px/150% arial ;}
</Style>
<Script type = "text/javascript">
Function (){
Var scrollTop;
Var scrollLeft;
If (typeof window. pageYOffset! = 'Undefined '){
ScrollTop = window. pageYOffset;
ScrollLeft = window. pageXOffset;
}
Else if (typeof document. compatMode! = 'Undefined' & document. compatMode! = 'Background '){
ScrollTop = document.doc umentElement. scrollTop;
ScrollLeft = document.doc umentElement. scrollLeft;
}
Else if (typeof document. body! = 'Undefined '){
ScrollTop = document. body. scrollTop;
ScrollLeft = document. body. scrollLeft;
}
Var scrollHeight = document.doc umentElement. scrollHeight;
Var scrollWidth = document.doc umentElement. scrollWidth;
Var clientWidth = document.doc umentElement. clientWidth;
Var clientHeight = document.doc umentElement. clientHeight;
Var offsetWidth = document.doc umentElement. offsetWidth;
Var offsetHeight = document.doc umentElement. offsetHeight;
Var screenTop = window. screenTop;
Var screenBottom = window. screenBottom;
Var screenLeft = window. screenLeft;
Var sheight = window. screen. height;
Var swidth = window. screen. width;
Var availHeight = window. screen. availHeight;
Var availWidth = window. screen. availWidth;
Document. getElementById ('scrolltop '). value = scrollTop;
Document. getElementById ('rollleft'). value = scrollLeft;
Document. getElementById ('rollheight'). value = scrollHeight;
Document. getElementById ('rollwidth'). value = scrollWidth;
Document. getElementById ('clientwidth'). value = clientWidth;
Document. getElementById ('clientheight '). value = clientHeight;
Document. getElementById ('offsetwidth'). value = offsetWidth;
Document. getElementById ('offsetheight'). value = offsetHeight;
Document. getElementById ('screentop '). value = screenTop;
Document. getElementById ('screenbottom '). value = screenBottom;
Document. getElementById ('screenleft'). value = screenLeft;
Document. getElementById ('sheight '). value = sheight;
Document. getElementById ('swidth'). value = swidth;
Document. getElementById ('availheight'). value = availHeight;
Document. getElementById ('availwidth'). value = availWidth;
}
</Script>
</Head>
<Body>
<Div style = "width: 420px; height: 470px; margin: 0 auto; font-size: 12px; border: solid 5px # ccc;">
<Center>
<Table width = "400" border = "0" cellspacing = "0" cellpadding = "0" style = "font-size: 12px; margin-top: 20px;">
<Tr>
<Td width = "187" align = "right"> scrollTop (the height of the scroll bar Volume): </td>
<Td width = "10"> </td>
<Td width = "209"> <input type = "text" name = "scrollTop" id = "scrollTop"/> </td>
</Tr>
<Tr>
<Td align = "right"> scrollLeft (the width of the scroll bar over): </td>
<Td> </td>
<Td> <input type = "text" name = "scrollLeft" id = "scrollLeft"/> </td>
</Tr>
<Tr>
<Td align = "right"> scrollHeight (actual content height): </td>
<Td> </td>
<Td> <input type = "text" name = "scrollHeight" id = "scrollHeight"/> </td>
</Tr>
<Tr>
<Td align = "right"> scrollWidth (actual content width): </td>
<Td> </td>
<Td> <input type = "text" name = "scrollWidth" id = "scrollWidth"/> </td>
</Tr>
<Tr>
<Td align = "right"> clientWidth (visible area width): </td>
<Td> </td>
<Td> <input type = "text" name = "clientWidth" id = "clientWidth"/> </td>
</Tr>
<Tr>
<Td align = "right"> clientHeight (visible area height): </td>
<Td> </td>
<Td> <input type = "text" name = "clientHeight" id = "clientHeight"/> </td>
</Tr>
<Tr>
<Td align = "right"> offsetWidth (add the scroll bar width ?) : </Td>
<Td> </td>
<Td> <input type = "text" name = "offsetWidth" id = "offsetWidth"/> </td>
</Tr>
<Tr>
<Td align = "right"> offsetHeight (add the height of the scroll bar ?) : </Td>
<Td> </td>
<Td> <input type = "text" name = "offsetHeight" id = "offsetHeight"/> </td>
</Tr>
<Tr>
<Td align = "right"> screenTop: </td>
<Td> </td>
<Td> <input type = "text" name = "screenTop" id = "screenTop"/> </td>
</Tr>
<Tr>
<Td align = "right"> screenBottom: </td>
<Td> </td>
<Td> <input type = "text" name = "screenBottom" id = "screenBottom"/> </td>
</Tr>
<Tr>
<Td align = "right"> screenLeft: </td>
<Td> </td>
<Td> <input type = "text" name = "screenLeft" id = "screenLeft"/> </td>
</Tr>
<Tr>
<Td align = "right"> sheight (High Resolution): </td>
<Td> </td>
<Td> <input type = "text" name = "sheight" id = "sheight"/> </td>
</Tr>
<Tr>
<Td align = "right"> swidth (resolution width): </td>
<Td> </td>
<Td> <input type = "text" name = "swidth" id = "swidth"/> </td>
</Tr>
<Tr>
<Td align = "right"> availHeight: </td>
<Td> </td>
<Td> <input type = "text" name = "availHeight" id = "availHeight"/> </td>
</Tr>
<Tr>
<Td align = "right"> availWidth: </td>
<Td> </td>
<Td> <input type = "text" name = "availWidth" id = "availWidth"/> </td>
</Tr>
</Table>
<A href = "javascript: a ()" style = "height: 20px; display: block;"> the content height is 400PX. Click to view all attribute values. </a>
</Center>
</Div>
</Body>
</Html>

In fact, we can use document.doc umentElement instead of document. body to get the desired result. Replace document.bodyin the Code with document.doc umentElement, and then look at the actual results in various browsers:
The result of the II test shows that the document.doc umentElement attribute is correctly interpreted by the IE series browser and the offsetHeight attribute is interpreted as scrollHeight by other standard browsers. Firefox and netscape have changed these two attributes. But in general, each attribute can have a corresponding explanation, not just as there are two poor interpretations like document. body.

Finally, you can safely use the JS method to obtain various page width attributes. ^_^!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.