473,705 Members | 4,203 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 #1
14 12534
Simon Wigzell wrote:
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;
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.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
Simon Wigzell wrote:
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!


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

Why wont an anchor work?

--
Rob
Jul 23 '05 #3
Simon Wigzell wrote:
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!


Try this...

<script type="text/javascript">

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

window.onload = scrollDiv;

</script>

Jul 23 '05 #4
<snip>

Thank you, that does the trick.
Jul 23 '05 #5
<snip>
Jul 23 '05 #6
RobB wrote:
[...]
Try this...

<script type="text/javascript">

function scrollDiv()
{
var el;
if ((el = document.getEle mentById('MyDiv Id'))
&& ('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><ti tle>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="hitBott om();">

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

--
Rob
Jul 23 '05 #7
RobG wrote:
RobB wrote:
[...]
Try this...

<script type="text/javascript">

function scrollDiv()
{
var el;
if ((el = document.getEle mentById('MyDiv Id'))
&& ('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.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

Jul 23 '05 #8
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
Jul 23 '05 #9

"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;

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!
Jul 23 '05 #10

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

Similar topics

13
3397
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
2507
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
84606
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
14465
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
5147
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
37149
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
3811
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
5361
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
10045
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
8690
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7895
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
5933
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
4440
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
4704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.