Thursday, January 3, 2008

document.body.scrollTop in IE

It seems that document.body.scrollTop does not work in IE6 if the document's doctype is defined as
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
To find out the scrolling offset in a way that works for both DTD3 and DTD4.01 you can use
Same goes for scrollLeft.
See more at document.body.scrollTop in IE


  1. It works awful, thanks a lot.
    My function for update position of an element with mouse movement (after your excellent contribution) looks like:

    function updatePos(e){
    var objFlow = document.getElementById('sumarizado');
    if(document.captureEvents){ = (e.pageX - 250) + "px"; = (e.pageY) + "px";
    }else{ = event.clientX + (document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft) - 250; = event.clientY + (document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);

  2. You are a complete life-saver - thanks.

  3. excellent solution, thanks

  4. thanks, used this like this....


    var scrollTop = (document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
    alert( scrollTop + " px");


    ie, chrome, and firefox all worked


    onto saving as php variable and post back to self to remember where user was on form when submit occured


[Due to much spam, comments are now moderated and will be posted after review]