Welcome to WebmasterWorld Guest from 54.146.139.201

Forum Moderators: incrediBILL

Message Too Old, No Replies

Why docType affects clientHeight?

     
3:47 pm on Sep 26, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2004
posts:219
votes: 0



<!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" lang="en-US" xml:lang="en-US">
<head>
<title>clientHeight</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</HEAD>
<BODY>
<script language=Javascript>
alert('h=' + document.body.clientHeight);
</script>
</body>
</html>

The clientHeight is 0. If removing the doctype or adding

<?xml version="1.0" encoding="iso-8859-1"?>
at the very beginning, the clientHeight is right. Why the docType affects the clientHeight?
1:01 am on Sept 27, 2005 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
posts:9074
votes: 6


The addition of a doctype switches the browser (I assume you are talking about IE6 in this case) into Standards-compliance mode - see the following thread Quirks Mode vs. Standards Mode [webmasterworld.com] for a general overview of the situation. Adding an XML prolog triggers a bug in IE6 which triggers quirks mode too.

As you can see, not just markup and CSS behave a bit differently in standards mode, but Javascript can be affected to. However, it is still better to stick with standards mode and try to find out the exact cause of the problem.

5:23 pm on Sept 27, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2004
posts:219
votes: 0


The problem is also reproduced in FF (1.07 on windows). I searched a lot, but didn't find an anwser yet. :-(
5:37 pm on Sept 27, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member suzyuk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Oct 1, 2002
posts:5199
votes: 0


as encyclo says it is due to Quirks Mode (when you remove the doctype or add the xml declaration)

Most (if not all?) browsers have a Quirks Mode and though they don't all render everything like IE while in Quirks Mode, Opera is close.

BUT one of the the main differences when your browser is rendering in Quirks Mode is that the BODY Element is the root element, so therefore is the same as the viewport as far as the initial containing block (ICB) is concerned, so if you were absolutely positioning something relative to the root you are using the BODY height/width co-ordinates.

however in Strict rendering mode the HTML element is the root element, the ICB, and if you're positiong absolutely to the viewport in this case it, the HTML element takes it height from the viewport and any positioning is done according to it's dimensions. The Body element in strict Mode will not have a height until you give it one.

Suzy

5:56 pm on Sept 27, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


It's not that the clientheight property on its own has changed -- it's that clientheight as a CHILD OF THE BODY ELEMENT is being assigned a different value in Standards Mode.

Here's how I understand it -- in Standards Mode, all the window and scrolling measures as we knew them now have been assigned to the child of the documentElement object -- and these specific measures are no longer assigned to children of the body object, as they were in Quirks Mode.

So the values for these children of document.body are now assigned from the size of the document, rather than from the size of browser window. This makes good sense to me, even though we are accustomed to the previous arrangement and this newer one is a bit unsettling. In your example,for instance, there IS no content in the body, and therefore zero height.

6:00 pm on Sept 27, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


I think we're saying the same thing - only you type faster and explain better, Suzy
8:36 pm on Sept 27, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2004
posts:219
votes: 0


Thank you both. Now I understood! :-)