I've been using html without the DOCTYPE spec. That worked fine, but I couldn't position a table correctly in css in IE. From another discussion on the forum, it was recommended to use the following DOCTYPE specification, and tables would position correctly: So, I did that. It worked great for positioning the table. However, with this DOCTYPE, 'document.body.scrollTop' in IE reports zero, no matter where the body is scrolled. Yeesh. I can get around it because I need to position the table in one page (and can specify the DOCTYPE), and I rely on the scrollTop value on another page (and can leave the DOCTYPE off that page). Not an optimal or elegant solution & besides, I'm going to need the scrollTop value in both pages, later on. Sooo - Questions: Is there a better construct/value to use to get the equivalent of 'document.body.scrollTop' Is there another DOCTYPE that does both correctly? Any thoughts appreciated. rickb
Here it is: <script language="javascript"> function LoadIFrame(jpgfile) { var iframe = document.getElementById("bigframe"); iframe.src = "/scribi/lib/IFrameContents.php?fn=" + jpgfile; iframe.style.left=10; iframe.style.top = document.body.scrollTop + 10; iframe.style.display = "block"; } If I use the transitional 4.01 doctype, document.body.scrollTop always returns zero (in Win IE 6), no matter the scrolling of the page (works fine in FF, Opera.) Actually, I got both situtations to work reasonably well by reverting to the 3.2 doctype. So, I'm moiving on. rickb
It seems that scrollTop only works if the overflow property is set: <div style="height: 100px; width: 100px; overflow: scroll;" onclick="alert(this.scrollTop)"> 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 </div> J.D.
THANK YOU ALL! I never would have learned why document.body.scrollTop wasn't working right -- I changed it to a 3.2 doctype and all is well. The wonders of search engines.
IE6 and DTD 4.01 requires you to use document.documentElement in stead of document.body, so u will need somthing like the below: if (document.documentElement && !document.documentElement.scrollTop) // IE6 +4.01 but no scrolling going on else if (document.documentElement && document.documentElement.scrollTop) // IE6 +4.01 and user has scrolled else if (document.body && document.body.scrollTop) // IE5 or DTD 3.2
hi..guys.. i have a problem here... the problem is like this.... i have managed to create a chat application using PHP with the auto refresh... for the chatting screen, i have used layer to contain all the user chatting messages..as u know..when user keep on typing and sending messages..the layer will eventually and automatically create a vertical scroll... To retrieve the data from the database...i need to keep the chatting screen(layer page) to be frefreshed in every several seconds... the problem is..when it is refreshing....the scroll bar in the layer will also be refreshed and the screen will scroll up back to the top of the messages... so, i wanna ask who knows how to make the scrollbar not to back to top...but stay at the bottom (when page is refreshing)..so that the user can see the latest message they typed... any javascript methods or other functions can help this? thanks... ---------------------------------------------------------------------- i have heard some reccomendation to use the document.objectelement.scrollTop method n i try to use it...but i wont works... this is the sample scenario... (let's say i want to make the distance become 10000 from the top (scenario 2) scenario 1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script language="JavaScript" type="text/JavaScript"> <!-- function scrollUp() { document.Layer1.scrollTop = Layer1.scrollHeight; } //--> </script> <title>Untitled Document</title> <META HTTP-EQUIV="refresh" content="2;URL=http://localhost/project/ww.php"> </head> <body onload = "scrollUp()"> <div id="Layer1" style="position:absolute; left:-1px; top:1px; width:600px; height:100px; z-index:1; overflow: scroll; visibility: visible; background-color: #CCFFFF; layer-background-color: #CCFFFF; border: 1px"> 1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br> </div> </body> </html> scenario 2 : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script language="JavaScript" type="text/JavaScript"> <!-- function scrollUp() { document.Layer1.scrollTop = 10000; } //--> </script> <title>Untitled Document</title> <META HTTP-EQUIV="refresh" content="2;URL=http://localhost/project/ww.php"> </head> <body onload = "scrollUp()"> <div id="Layer1" style="position:absolute; left:-1px; top:1px; width:600px; height:100px; z-index:1; overflow: scroll; visibility: visible; background-color: #CCFFFF; layer-background-color: #CCFFFF; border: 1px"> 1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br>1<br> </div> </body> </html> anyone can tell me how to properly use taht method? pls .... thanks....
Just wanted to thank you, you saved my day! I struggled with non working js code to get the mouse position, but now it works like a charm! - Niklas
Can anybody tell... why is this not working in IE but Firefox? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head> <title>Pure CSS Scrollable Table with Fixed Header</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="language" content="en-us" /> <style type="text/css"> <!-- /* define height and width of scrollable area. Add 16px to width for scrollbar */ div.tableContainer { clear: both; border: 1px solid #963; height: 285px; overflow: auto; width: 756px } /* Reset overflow value to hidden for all non-IE browsers. */ html>body div.tableContainer { overflow: hidden; width: 756px } /* define width of table. IE browsers only */ div.tableContainer table { float: left; width: 740px } /* define width of table. Add 16px to width for scrollbar. */ /* All other non-IE browsers. */ html>body div.tableContainer table { width: 756px } /* set table header to a fixed position. WinIE 6.x only */ /* In WinIE 6.x, any element with a position property set to relative and is a child of */ /* an element that has an overflow property set, the relative value translates into fixed. */ /* Ex: parent element DIV with a class of tableContainer has an overflow property set to auto */ thead.fixedHeader tr { position: relative } /* set THEAD element to have block level attributes. All other non-IE browsers */ /* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */ html>body thead.fixedHeader tr { display: block } /* make the TH elements pretty */ thead.fixedHeader th { background: #C96; border-left: 1px solid #EB8; border-right: 1px solid #B74; border-top: 1px solid #EB8; font-weight: normal; padding: 4px 3px; text-align: left } /* define the table content to be scrollable */ /* set TBODY element to have block level attributes. All other non-IE browsers */ /* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */ /* induced side effect is that child TDs no longer accept width: auto */ html>body tbody.scrollContent { display: block; height: 262px; overflow: auto; width: 100% } --> </style> <script language="javascript"> var speed = 5; function scroll(theDiv) { var div1 = document.getElementById(theDiv); //alert(div1.scrollHeight); div1.scrollTop = div1.scrollTop + speed; //div.scrollTop = div.scrollHeight; t1=setTimeout("scroll('"+theDiv+"')",1000); } function getval(theDiv) { var div1 = document.getElementById(theDiv); alert(div1.scrollTop); } </script> </head> <body onload="scroll('news')"> <div id="tableContainer" class="tableContainer"> <table border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable" class="scrollTable"> <thead class="fixedHeader"> <tr> <th><a href="#">Header 1</a></th> <th><a href="#">Header 2</a></th> <th><a href="#">Header 3</a></th> </tr> </thead> <tbody class="scrollContent" id="news"> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>And Repeat 1</td> <td>And Repeat 2</td> <td>And Repeat 3</td> </tr> <tr> <td>Cell Content 1</td> <td>Cell Content 2</td> <td>Cell Content 3</td> </tr> <tr> <td>More Cell Content 1</td> <td>More Cell Content 2</td> <td>More Cell Content 3</td> </tr> <tr> <td>Even More Cell Content 1</td> <td>Even More Cell Content 2</td> <td>Even More Cell Content 3</td> </tr> <tr> <td>End of Cell Content 1</td> <td>End of Cell Content 2</td> <td>End of Cell Content 3</td> </tr> </tbody> </table> </div> <input type="button" value="Get ScrollTop" onclick="getval('news');"> <script language="javascript">document.getElementById("news").scrollTop = 1;</script> </body></html>
@gullam18 : Internet Explorer does not understand the child selector command html>body thead.fixedHeader tr { display: block } You can read up more on hacks and tricks needed to get certain functionality to work in Internet Explorer here: http://www.webcredible.co.uk/user-friendly-resources/css/hacks-browser-detection.shtml