Forum Moderators: open

Message Too Old, No Replies

Trying to read scrollTop different in IE6 quirks vs. compliant

         

Lance

2:18 am on Aug 20, 2004 (gmt 0)

10+ Year Member



I'm trying to read the scrollTop property to use for a CSS expression in IE.

It seems I can read the property this way in (IE6)compliant mode:

document.documentElement.scrollTop

and this way in quirks mode (no!DOCTYPE):

document.body.scrollTop

Is there any way to read this property that will work both ways?

Thanks.

Rambo Tribble

3:40 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, maybe I don't understand, but this gives me the same result with or without a
!DOCTYPE declaration: 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body onload="document.frmOne.taOne.scrollTop=40;">
<form action="" name="frmOne">
<p>
<textarea rows="5" cols="40" name="taOne">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec eget metus in pede feugiat porttitor.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent dui lorem,
adipiscing ac, hendrerit ut, pretium et, felis. Donec id mauris. Phasellus malesuada mollis quam. Nulla
condimentum ultricies quam. Vivamus velit ipsum, dapibus vitae, ullamcorper non, vulputate pharetra, eros.
Donec at metus. Phasellus odio lectus, gravida sit amet, placerat sagittis, convallis non, purus. Sed tincidunt
eros id augue. Etiam tempus gravida turpis.
</textarea>
</p>
<button onclick="alert(document.frmOne.taOne.scrollTop);">Whoa, Nellie!</button>
</form>
</body>
</html>

Lance

11:37 am on Aug 20, 2004 (gmt 0)

10+ Year Member



It seems it wasn't scrollTop that was breaking for me, it is document.documentElement vs. document.body. In IE < 6, document.body is the root element, and therefore the owner of the scroll bars. In IE 6 however, the <html> element is the root element. Guess how you access it? Using document.documentElement.

Since I am trying to read the location of the document, within the viewport, I need to reference the appropriate root for the browser version.

Two lines of code to resolve, >4 hours to troubleshoot. *sigh*

Lance

12:41 pm on Aug 20, 2004 (gmt 0)

10+ Year Member



I found a better cross browser solution. Reading "object.offsetParent.scrollTop" will return the scrollTop position of the parent container that owns the scroll bars regardless of what it is. Using this method, one rule will work for IE in either mode.