Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Scrolling

  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Scrolling

    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

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by HaydenGriffiths; 07-14-2005 at 08:09 AM.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm try this
    Code:
    <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
    Last edited by _Aerospace_Eng_; 07-14-2005 at 08:30 AM.

  • #5
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
      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

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by HaydenGriffiths
    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

    Code:
      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.

  • #7
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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):
    Code:
    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.

  • #9
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by HaydenGriffiths
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •