Force scroll on a <div>?

S

Simon Wigzell

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.MyDivId.scrollTop = document.all.MyDivId].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!
 
R

Randy Webb

Simon said:
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.MyDivId.scrollTop = document.all.MyDivId].scrollHeight;

No it doesn't, you just think it does.
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!

Yep, its possible.
 
R

RobG

Simon said:
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.MyDivId.scrollTop = document.all.MyDivId].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!

Nor will it work in any browser that doesn't support Microsoft's
proprietary document.all.

Why wont an anchor work?
 
R

RobB

Simon said:
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.MyDivId.scrollTop = document.all.MyDivId].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!

Try this...

<script type="text/javascript">

function scrollDiv()
{
var el;
if ((el = document.getElementById('MyDivId'))
&& ('undefined' != typeof el.scrollTop))
{
el.scrollTop = 0;
el.scrollTop = 5000;
}
}

window.onload = scrollDiv;

</script>
 
R

RobG

RobB wrote:
[...]
Try this...

<script type="text/javascript">

function scrollDiv()
{
var el;
if ((el = document.getElementById('MyDivId'))
&& ('undefined' != typeof el.scrollTop))
{
el.scrollTop = 0;
el.scrollTop = 5000;
}
}
[...]

But that presumes that the div goes all the way to the bottom of
the page...

Here's an attempt at using an automated anchor. It's a bit
rough, but I don't know enough about CSS (or perhaps even JS) to
make it work properly. See if you can make it better...

<html><head><title>play</title>
<style type="text/css">
.upper {color: #333366; background-color: #ddddff;
position: relative; height: 2000px;}
.lower {position: absolute; bottom: 0; visibility: hidden;}
.other {color: #333366; background-color: #ddddff;
position: absolute; top: 2020px; height: 2000px;
width:100%;}
</style>
<script type="text/javascript">
function hitBottom() {

// the '' seems to be needed to make x a string
var x = window.location + '';
if (x.indexOf('#') == '-1') {
window.location = window.location + '#bottom';
}
// I'd like to put an else here to just reload the page,
// but it causes an endless loop of reloading
}
</script>

</head><body>
</head><body onload="hitBottom();">

<div class="upper" style="border: 5px groove gold;">
<a href="#bottom">Here is the div</a><br>
<div class="lower"><br>
<a name="bottom">fred</a>
</div>
<br>
<div class="other">
hi
</div>
</body></html>
 
R

RobB

RobG said:
RobB wrote:
[...]
Try this...

<script type="text/javascript">

function scrollDiv()
{
var el;
if ((el = document.getElementById('MyDivId'))
&& ('undefined' != typeof el.scrollTop))
{
el.scrollTop = 0;
el.scrollTop = 5000;
}
}
[...]

But that presumes that the div goes all the way to the bottom of
the page...

(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.scrollTop 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
 
R

RobG

RobB said:
(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.scrollTop 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><title>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.getElementById('MyDivId');
if ('undefined' != typeof el.scrollTop) {
el.scrollTop = '0px';
el.scrollTop = '5000px';
} else {
alert('scrollTop bzzzt');
}
}
</script>

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

<div class="upper" id="MyDivId">
<a href="#bottom">Link to bottom</a><br>
<button onclick="scrollDiv();">scrollDiv</button>
<div class="lower"><br>
<a name="bottom">bottom</a>
</div>
</div>
<br>
<div class="other">
other div
</div>
</body></html>
 
S

Simon Wigzell

RobG said:
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><title>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.getElementById('MyDivId');
if ('undefined' != typeof el.scrollTop) {
el.scrollTop = '0px';
el.scrollTop = '5000px';
} else {
alert('scrollTop bzzzt');
}
}
</script>

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

<div class="upper" id="MyDivId">
<a href="#bottom">Link to bottom</a><br>
<button onclick="scrollDiv();">scrollDiv</button>
<div class="lower"><br>
<a name="bottom">bottom</a>
</div>
</div>
<br>
<div class="other">
other div
</div>
</body></html>

How does all this differ from my original method:

document.all.MyDivId.scrollTop = 5000;

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 just thought there would be some easy 1 line command, browser
independent, to set a scroll bar to the end!
 
R

RobG

Simon Wigzell wrote:
[...]
How does all this differ from my original method:

document.all.MyDivId.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.getElementById will work back to about IE 5.5.

Have a look at the FAQ and dynWrite:

<URL:http://www.jibbering.com/faq>

Or just include something like:

if (document.getElementById) {
var x = document.getElementById('aRef');
} else {
var x = document.all('aRef');
}

Numerous shorthand versions of the above abound in posts here.

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

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

Hmmm.
 
R

RobB

RobG said:
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.getElementById('MyDivId');
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.getElementById
&& (el = document.getElementById('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...
 
R

RobB

Simon said:
How does all this differ from my original method:

document.all.MyDivId.scrollTop = 5000;

It differs in that Element.scrollHeight 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...what 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.
 
S

Simon Wigzell

You really need to learn how to communicate...what 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.MyDivId.scrollTop = document.all.MyDivId].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.MyDivId.scrollTop = 5000

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.ScrollBottom()

or something. Geez!
 
R

RobG

RobB wrote:
[...]
(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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top