IE7 and document.body.scrollTop

After being nagged by my Windows Automatic Updates for quite some time, I finally decided to give in and just install the IE7 update. I made a mistake by not backing up my IE6 files, or trying to find a method to have IE6 co-exist with IE7.

Anyway, I found that the document.body.scrollTop method to find out how much of the page has been scrolled down doesn’t work anymore. Now I have to use document.documentElement.scrollTop. And since the only difference of IE7 and IE6 is reflected inside a long string in the navigator.appVersion or navigator.appName objects, a little Regular Expression has to be built.

Here is the snip I ended up with, to cater both IE6 and IE7:

switch(navigator.appName)
{
  case 'Microsoft Internet Explorer':
    var myregex = /MSIE 7\.0/i;
    var myArray = navigator.appVersion.match(myregex);
    if(myArray.length > 0) scrollY = document.documentElement.scrollTop;
    else scrollY = document.body.scrollTop;
  break;
  default:
    scrollY = window.pageYOffset;
}

Anyone found a better solution?

0 Shares

One thought on “IE7 and document.body.scrollTop”

  1. Yes there is a much better way. Using JSON notation you can simple evaluate which document property exists then throw the value.

    The expression before the question mark is what’s evaluated true or false. If true it executes the expression/value after the question mark. If false it executes the expression/value after the colon.

    The line below has nested logic that loops through each condition to see if the browser supports it.

    (window.pageYOffset)?window.pageYOffset:(document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop;

Comments are closed.