document.documentElement.scrollTop in IE7

V

vunet.us

The JS line below is supposed to return the number of pixels from the
very top of the page to the top of the scrolled-down position of this
page. I've noticed that IE7 always return 0 even though I scrolled
down a lot. Firefox and IE6 show over 1000 in my tests. Did something
change in IE7?

var topPosFix = typeof window.pageYOffset != 'undefined' ?
window.pageYOffset:document.documentElement &&
document.documentElement.scrollTop ?
document.documentElement.scrollTop: document.body.scrollTop?
document.body.scrollTop:0;
 
M

Martin Honnen

The JS line below is supposed to return the number of pixels from the
very top of the page to the top of the scrolled-down position of this
page. I've noticed that IE7 always return 0 even though I scrolled
down a lot. Firefox and IE6 show over 1000 in my tests. Did something
change in IE7?

var topPosFix = typeof window.pageYOffset != 'undefined' ?
window.pageYOffset:document.documentElement &&
document.documentElement.scrollTop ?
document.documentElement.scrollTop: document.body.scrollTop?
document.body.scrollTop:0;

With IE 6 and 7 you should use document.documentElement.scrollTop if
document.compatMode != 'BackCompat'.
Not sure why you get 0, can you post a URL?
 
V

vunet.us

With IE 6 and 7 you should use document.documentElement.scrollTop if
document.compatMode != 'BackCompat'.
Not sure why you get 0, can you post a URL?

Here is copy/paste example for demonstration. Sorry I am able to get
to external server in the evening only...
So, IE7 shows 0:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Untitled Document</title>
<script>

function fff(){
//fix top positioning
var topPosFix = typeof window.pageYOffset != 'undefined' ?
window.pageYOffset:document.documentElement &&
document.documentElement.scrollTop ?
document.documentElement.scrollTop: document.body.scrollTop?
document.body.scrollTop:0;


alert('document.documentElement.scrollTop='+document.documentElement.scrollTop
+', document.body.scrollTop='+document.body.scrollTop)
}

</script>
</head>

<body onLoad="fff();">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>
 
V

vunet.us

Here is copy/paste example for demonstration. Sorry I am able to get
to external server in the evening only...
So, IE7 shows 0:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Untitled Document</title>
<script>

function fff(){
//fix top positioning
var topPosFix = typeof window.pageYOffset != 'undefined' ?
window.pageYOffset:document.documentElement &&
document.documentElement.scrollTop ?
document.documentElement.scrollTop: document.body.scrollTop?
document.body.scrollTop:0;

alert('document.documentElement.scrollTop='+document.documentElement.scrollTop
+', document.body.scrollTop='+document.body.scrollTop)

}

</script>
</head>

<body onLoad="fff();">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>

So can anybody confirm this problem or is it something wrong with me?
Thanks.
 
D

David Mark

So can anybody confirm this problem or is it something wrong with me?

The problem is that you are checking the position on load. Get rid of
the onload attribute and add this to your script:

window.onscroll = fff;
 
D

David Mark

With IE 6 and 7 you should use document.documentElement.scrollTop if
document.compatMode != 'BackCompat'.
Not sure why you get 0, can you post a URL?

True enough, but I think the OP's test is more robust. Assuming that
all browsers that support document.compatMode also populate the
scrollTop property of the documentElement as expected seems a logical
assumption, but I wouldn't rely on it.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top