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
(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)
Same goes for scrollLeft.
See more at document.body.scrollTop in IE

5 comments:

  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){
    objFlow.style.left = (e.pageX - 250) + "px";
    objFlow.style.top = (e.pageY) + "px";
    }else{
    objFlow.style.left = event.clientX + (document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft) - 250;
    objFlow.style.top = event.clientY + (document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
    }
    }

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

    ReplyDelete
  3. excellent solution, thanks

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


    $("button").click(function(){

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


    });


    ie, chrome, and firefox all worked

    thanks!


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

    ReplyDelete

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