...

View Full Version : Scrolling



HaydenGriffiths
07-14-2005, 12:33 AM
Hey, I was looking for a little help, I am writing this application in Delphi, which is going to be used for a conference. It is an HTTP server, which will frequently update a page, on the page it has a whole heep of names, of people who need to report to the admin.

So ive been looking around the net for the last couple of days, trying to write a JavaScript that, would load the page, scroll down to the end, refreash the page (get the new data) and repeat the cycle.

This is the code that I have so far.

<head>
<script language="JavaScript">
function pageScroll() {
var curpos = 0;
while (curpos < document.body.scrollHeight)
{
setTimeout("window.scrollBy(0,1)",50);
curpos = curpos + 1;
}
window.scroll(0,0);
window.location.reload();
}

</script></head>

<body onLoad="pageScroll()">
<p>some persons name</p>
</body>

But at the moment something very strange is happening, even if i remove the code, to move the page back up to the rop and refresh, it scrolls it about half way, stops, focuses, another window, and when i come back to the webpage it is at the end. I have also noticed by replacing the code after the loop with 'Alert("blah");' the even before it is at the end of the page it displays the alert, does JavaScript not wait until the loop is finished to execute the next line of code?

Could someone please help me out, this is doing my head in.

Thanks
Hayden

jscheuer1
07-14-2005, 05:50 AM
document.body.scrollHeight is inaccurate with most doctypes, try:

document.documentElement.scrollHeight

Also, using documentElement instead of body will cause problems with some older browsers. If that is a concern, there is a way around that. See if it helps first though.

HaydenGriffiths
07-14-2005, 06:58 AM
No it is still doing this wierd thing. I have been messing around with the page a bit, and after experimenting, with a different numbers of visible lines on the page, it would work fine, with 50 or less lines, but anymore than 50 it would, stop, and focus notepad.

Have you got any ideas of what would be causing this?

I am starting to think that it may be the way I am caculating, weather or not to keep scrolling, as I just re-installed Internet Explorer, and it did a similar thing, but sooner, and it didnt focus another window. Its al very strange.

Thanks for your help so far.

Hayden

_Aerospace_Eng_
07-14-2005, 07:26 AM
Hmm try this

<script type="text/javascript">
function scrollme(){
window.scrollTo(0,document['body'].scrollHeight);
setTimeout("window.location.reload()",10000);
}
</script>
It scrolls the browser window to as far as it can scroll and refreshes the page every 10 seconds 1000=1 second. I'm not too sure how many older browsers understand the square bracket notation. It seems to work on the latest browsers, e.g. IE6, FF 1.0.5, Opera 8.0, Opera 7.54, NS 8.0, NS 7.2

HaydenGriffiths
07-14-2005, 10:24 AM
Hey guys, ive finally got it, moved it into an if statement, I think there may have just been a few problems of adding the wrong numbers into the varibles (lol trust me to be so dumb)

Turn out something like this



ScrollScript : Array[1..18] Of String = (
'<script language="JavaScript">',
'var curpos = 0;',
'function pageScroll() {',
' if (curpos < document.documentElement.scrollHeight)',
' {',
' window.scrollBy(0,10);',
' setTimeout("pageScroll()",1);',
' curpos = curpos + 10;',
' }',
' else',
' {',
' window.scroll(0,0);',
' window.location.reload();',
' curpos = 0;',
' pageScroll();',
' }',
'}',
'</script>');


Thanks heeps for your words though
Hayden

_Aerospace_Eng_
07-14-2005, 11:12 AM
Hey guys, ive finally got it, moved it into an if statement, I think there may have just been a few problems of adding the wrong numbers into the varibles (lol trust me to be so dumb)

Turn out something like this



ScrollScript : Array[1..18] Of String = (
'<script language="JavaScript">',
'var curpos = 0;',
'function pageScroll() {',
' if (curpos < document.documentElement.scrollHeight)',
' {',
' window.scrollBy(0,10);',
' setTimeout("pageScroll()",1);',
' curpos = curpos + 10;',
' }',
' else',
' {',
' window.scroll(0,0);',
' window.location.reload();',
' curpos = 0;',
' pageScroll();',
' }',
'}',
'</script>');


Thanks heeps for your words though
Hayden
And your what appears to be bloated script scrolls the page to the very bottom and refreshes? Isn't that exactly what the script I posted does? Yeah I believe it does, and it doesn't count lines, it just automatically scrolls as far as it can scroll. Unless I'm missing something here I don't see the need for such an overzealous code.

HaydenGriffiths
07-14-2005, 01:32 PM
Mabey, you didnt read my first post properly, my web page, display names, around two thousand of them but it is not fixed, it could be one hundred or ten thousand, your code just scrolls to the end, refreshes after ten seconds and then goes back to the end, with out seeing anything that is in the middle, the code i posted is speed up a lot but, it displays everything that I need it to.

So to answer your rhetorical question, no you code does not do the same thing.

Hayden

jscheuer1
07-14-2005, 05:24 PM
Well, I'll just chuck this in since I see you are using the:

document.documentElement

thing. To make that backwards compatible, create a global function (top level):

function iecompattest(){
return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}Then, wherever 'document.documentElement' or 'document.body' are used in your code, substitute 'iecompattest()' This is transparent to other browsers yet allows older IE versions to work with your script and allows it to function in IE6 when there is no doctype on the page.

Credit for the above code snippet, I believe, belongs to Dynamic Drive. It and variations of it are used in many of their scripts for exactly the reasons just outlined.

HaydenGriffiths
07-14-2005, 11:13 PM
Thanks heeps jscheuer1, you've speed this up a whole heep for me. But this whole part of the project is done now. So ill see you when I decide to get into propery web design.

Hayden

_Aerospace_Eng_
07-14-2005, 11:14 PM
Mabey, you didnt read my first post properly, my web page, display names, around two thousand of them but it is not fixed, it could be one hundred or ten thousand, your code just scrolls to the end, refreshes after ten seconds and then goes back to the end, with out seeing anything that is in the middle, the code i posted is speed up a lot but, it displays everything that I need it to.

So to answer your rhetorical question, no you code does not do the same thing.

Hayden
Okay then, my apologies.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum