Forum Moderators: open

Message Too Old, No Replies

why would doctype Strict vs. Transitional affet use of:

scrollTop & pageYOffset

         

benlieb

10:02 pm on Mar 22, 2006 (gmt 0)

10+ Year Member



I've written a simple function to load stuff into an empty div in the top left corner of the VISIBLE window, no matter the scrolling.

Here is the function.


function show(thetext){
var tipobj=document.getElementById("tip");
tipobj.innerHTML = thetext;
tipobj.style.top = document.all? document.body.scrollTop + "px" : pageYOffset + "px";
tipobj.style.left = "0px";
tipobj.style.visibility = "visible";
}

It has worked fine until I put it on another client's site. The code then only loaded it in the top left (ie DISAPPEARS when scrolled down) in IE.

AFter 40 minutes of elimination, the only difference in the sites were Strict Doctype vs. Transitional.

Apparently the code only works in Transitional. Can someone enlighten me why?

DrDoc

10:10 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are both full doctypes matching one of the ones found here: [w3.org...]

Bernard Marx

10:43 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I seem to remember something about some relevant properties moving to document.documentElement when in strict mode.

So maybe something similar to:

var D = document; 
var scrollParent = D.compatMode && D.compatMode!= "BackCompat"
? D.documentElement
: D.body.scrollTop;
tipobj.style.top = (D.all? scrollParent.scrollTop:pageYOffset) + "px";

benlieb

4:12 am on Mar 23, 2006 (gmt 0)

10+ Year Member



Found some info as follows:

-------------------------

Microsoft decided that if you choose a Strict DOCTYPE that you must obtain the values from document.documentElement while if you choose a Transitional DOCTYPE or have no DOCTYPE at all that you must use document.body. Why they couldn't just populate both sets of fields like the other browsers do is anyone's guess. Having made the decision to only populate one set of fields they chose something completely unrelated to test for in order to decide which fields to populate.

So what does this mean for our Javascript coding? Well, if you don't use DOCTYPEs or use a Transitional DOCTYPE then you don't need to worry about document.documentElement and can keep using document.body.

Bernard Marx

10:58 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this is actually a move toward standardisation. In W3C terms,
document.body
doesn't exist, whilst
document.documentElement
does.