473,887 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Force scroll on a <div>?

I want to display HTML text inside a div and have the page open scrolled to
the bottom. (My div has overflow:auto so that it appears with scroll bars)

I have found this that works on a textarea:

document.all.My DivId.scrollTop = document.all.My DivId].scrollHeight;

But this doesn't work on a div. I am using a div because the text I am
displaying contains HTML. Surely it is possible to simply set a scroll bar
to the bottom in a div??? Thanks!
Jul 23 '05
14 12561
Simon Wigzell wrote:
[...]
How does all this differ from my original method:

document.all.My DivId.scrollTop = 5000;
Not greatly, my suggestion was to use a link which requires zero
JavaScript if the link to the page includes it. But it scrolls
the entire page to the anchor, which is likely not what you
wanted. I couldn't get my JS method to work reasonably anyway.

Except that it does a lot of compatibility checking first? (Which I am not
too concerned about as my clients are recommended to use IE anyway - don't
ask!) This method does not work on a div.
I couldn't get RobB's script to work at all in any browser - but
I'm not allowed access to the security settings of IE on my
Windows box so it may be that.

It is fairly trivial to include support for non-document.all
browsers. Given the rise in support for alternatives, I think
it's reasonable to include it anyway, regardless of your
client's (?) opinion.

Using document.getEle mentById will work back to about IE 5.5.

Have a look at the FAQ and dynWrite:

<URL:http://www.jibbering.c om/faq>

Or just include something like:

if (document.getEl ementById) {
var x = document.getEle mentById('aRef' );
} else {
var x = document.all('a Ref');
}

Numerous shorthand versions of the above abound in posts here.

var x;
if ( (x = document.getEle mentById('aRef) )
|| (x = document.getEle mentById('aRef' )) )

etc.

I just thought there would be some easy 1 line command, browser
independent, to set a scroll bar to the end!


Hmmm.
--
Rob
Jul 23 '05 #11
RobG wrote:
RobB wrote:
(snip)

Not presuming anything - I think one of us misunderstood the Q. This doesn't seem to involve scrolling the entire page, just a scrollable (CSS overflow: auto|scroll) element. Setting refToElement.sc rollTop to a high number, larger than it would ever need to be, seems to do the trick. The preceding 'reset' is necessary in moz, no idea why, but
otherwise the autoscroll only goes all the way to the bottom on initial load, falling short subsequently

Nope, understood the question, just not your response! :-)

Your code doesn't do anything in either Mozilla or IE for me...


(snip)

....probably because those divs have no content to speak of. Setting CSS
overflow to 'auto' won't have any effect if there isn't any overflow,
i.e. the rendered height of the element is greater than the scroll
height of any content. Again: you seem to be fixated on scrolling the
entire page, not the contents of an embedded div element.

function scrollDiv() { var el = document.getEle mentById('MyDiv Id');
if ('undefined' != typeof el.scrollTop) {
el.scrollTop = '0px';
el.scrollTop = '5000px';


(snip)

btw scroll properties are integers...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled </title>
<style type="text/css">

#foo {
width: 200px;
height: 500px;
margin: 40px auto;
overflow: auto;
}

</style>
<script type="text/javascript">

function scrollDiv()
{
var el;
if (document.getEl ementById
&& (el = document.getEle mentById('foo') )
&& ('undefined' != typeof el.scrollTop))
{
el.scrollTop = 0;
el.scrollTop = 5000;
}
}

onload = scrollDiv;

</script>
</head>
<body>
<div id="foo">
<script type="text/javascript">
x = 0, y = [];
while (x++ < 40)
y.push(x + '<hr />');
document.write( y.join(''));
</script>
</div>
</body>
</html>

I may have misinterpreted the OP's question - it was clumsily posed -
but one of us is barking up the wrong subtree...

Jul 23 '05 #12
Simon Wigzell wrote:
"RobG" <rg***@iinet.ne t.auau> wrote in message
news:OF******** *********@news. optus.net.au...
RobB wrote:
(snip)

Not presuming anything - I think one of us misunderstood the Q. This doesn't seem to involve scrolling the entire page, just a scrollable (CSS overflow: auto|scroll) element. Setting refToElement.sc rollTop to a high number, larger than it would ever need to be, seems to do the trick. The preceding 'reset' is necessary in moz, no idea why, but
otherwise the autoscroll only goes all the way to the bottom on initial load, falling short subsequently

Nope, understood the question, just not your response! :-)

Your code doesn't do anything in either Mozilla or IE for me,
even though no errors are produced and as far as I can tell,
everything seems OK. Maybe my CSS is crap?

Here's what I'm working with:

<html><head><ti tle>play</title>
<style type="text/css">
.upper {color: #333366; background-color: #ddddff;
position: relative; height: 2000px; overflow: auto;
border: 5px groove red;}
.lower {position: absolute; bottom: 0px;}
.other {color: #334433; background-color: #ddddff;
position: absolute; top: 2020px; height: 2000px;
width:100%;}
</style>
<script type="text/javascript">
function scrollDiv() {
var el = document.getEle mentById('MyDiv Id');
if ('undefined' != typeof el.scrollTop) {
el.scrollTop = '0px';
el.scrollTop = '5000px';
} else {
alert('scrollTo p bzzzt');
}
}
</script>

</head><body>
</head><body>

<div class="upper" id="MyDivId">
<a href="#bottom"> Link to bottom</a><br>
<button onclick="scroll Div();">scrollD iv</button>
<div class="lower">< br>
<a name="bottom">b ottom</a>
</div>
</div>
<br>
<div class="other">
other div
</div>
</body></html>
--
Rob


How does all this differ from my original method:

document.all.My DivId.scrollTop = 5000;


It differs in that Element.scrollH eight may return false (understated)
values if you call this onload, presumably because of timing issues (a
small delay fixes it). The high value insures that the element will
scroll to the end.
Except that it does a lot of compatibility checking first? (Which I am not too concerned about as my clients are recommended to use IE anyway - don't ask!) This method does not work on a div.
You really need to learn how to communicate...w hat does 'This method
does not work on a div' mean? What method? What 'div'? Jeez...
I just thought there would be some easy 1 line command, browser
independent, to set a scroll bar to the end!


JS doesn't do commands. A tiny script is hardly anything to get
exercised about.

Jul 23 '05 #13
<snip>>
You really need to learn how to communicate...w hat does 'This method
does not work on a div' mean? What method? What 'div'? Jeez...

If you go back to the original post I state that I have tried :

document.all.My DivId.scrollTop = document.all.My DivId].scrollHeight;

and it does not work with a div, but does with a text area. The guts of the
solution posated by ROB amount to:

document.all.My DivId.scrollTop = 5000

I just thought there would be some easy 1 line command, browser
independent, to set a scroll bar to the end!


JS doesn't do commands. A tiny script is hardly anything to get
exercised about.

Well, I haven't found a tiny script that works yet! You'd think there would
be something like:

document.MyForm .MyDivId.Scroll Bottom()

or something. Geez!
Jul 23 '05 #14
RobB wrote:
[...]
Your code doesn't do anything in either Mozilla or IE for me...

(snip)

...probably because those divs have no content to speak of. Setting CSS
overflow to 'auto' won't have any effect if there isn't any overflow,
i.e. the rendered height of the element is greater than the scroll
height of any content. Again: you seem to be fixated on scrolling the
entire page, not the contents of an embedded div element.


Yup. So my understanding of CSS was somewhat lacking (as
expected).

function scrollDiv() {
[...] </html>
Thanks for the working example, neat. Now if I could ever find
a use for it...

I may have misinterpreted the OP's question - it was clumsily posed -
but one of us is barking up the wrong subtree...


No, just unable to implement a solution that didn't scroll the
page. Thanks again for persisting. :-)

--
Rob
Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3412
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to <foo/>) From XHTML specification:
1
2516
by: Philo | last post by:
How do I select all <div> tags except those which contain a <table> tag somewhere within them? Example XML: <********************** sample input ***********************> <txtSectionBody> <div> <span>
3
84643
by: Paul Thompson | last post by:
When I put a <div ...> inside a <table> specification, functionality is not there. When I put the <table> inside the <div> everything works. Why is that?
8
14474
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a page, there is a huge gap (like 3/8 inch or 25 px) between them. This is driving me bananas. What the hey am I missing? dh ------------------------------------------------ Dan Hansen ------------------------------------------------
8
5169
by: slim | last post by:
hi again all, i am still working on the website as mentioned in earlier threads and have hit another snag... http://awash.demon.co.uk/index.php http://awash.demon.co.uk/vd.css the php is pulling a name and placing it under the thumbnail (the text is
6
37165
by: Marcus Otmarsen | last post by:
Ok, the situation is as follows: I defined a <DIV> like: <div id="myid" style="position: absolute; height: 10; width: 10;"><img src="images/object.gif" height=10 width=10></div> This pane is used to move an object around on the web page by a javascript. As long as I define no further <div> pane everything works fine.
3
3817
by: Josef K. | last post by:
Asp.net generates the following html when producing RadioButton lists: <td><input id="RadioButtonList_3" type="radio" name="MyRadioButtonList" value="644" onclick="__doPostBack('SitesRadioButtonList_3','')" language="javascript" />
28
5392
by: Kent Feiler | last post by:
1. Here's some html from a W3C recommendations page. <P>aaaaaaaaa<DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</DIV> 2.Although I didn't think it would make any difference, I tried it with the </p>s included as well. <P>aaaaaaaaa</p><DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</p></DIV>
8
10059
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a state where it almost passes the W3C Markup Validation Service. However, the one error, Error Line 166,...
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.