Using Javascript to autoscroll PHP in textbox

Discussion in 'JavaScript' started by soma56, Jun 29, 2010.

  1. #1
    My problem seems like a common one. I've searched on the web and cannot find an answer to my problem. Basically, I'm updating the contents of a textbox using php. The problem is that when the text is being updated it does not autoscroll down so that users can see the new content. My php script runs through a loop and new content is being added. While the new content is being added to the text box successfully - the textbox does not automatically scroll down and/or flow with the new content. I've been told that Javascript can solve this problem.

    Here is an overview of what I'm trying to do:

    
    <div id="divExample" style="width: 315px; height: 200px; overflow:auto; border: 2px solid black;">
    
    <?PHP MY PHP CODE LOOPING AND ECHOING RESULTS?>
    
    </div>
    
    Code (markup):
    And this is what I think needs to be done in order to continuously ensure that the textbox scrolls to the bottom as new output arrives:

    
    function scrollCheck() {
       var chatBox = document.getElementById('divExample');
       var top = chatBox.scrollTop;
       var height = chatBox.scrollHeight - 150;
       
       var returnText;
       if ((top == height) || (height < 20)) {
           return true;
       } else {
           return false;
       }
    }
    
    <div id="divExample" style="width: 315px; height: 200px; overflow:auto; border: 2px solid black;">
    
    <?PHP MY PHP CODE LOOPING 
    AND ECHOING RESULTS
    Call the scrollCheck function 
    ?>
    
    </div>
    
    
    Code (markup):
    Am I on the right track? Does anyone know of a working similar example? Would there be a more effective way to accomplish this?
     
    soma56, Jun 29, 2010 IP
  2. computersx

    computersx Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use the following code snippet:

    which will automatically scroll to the end.
     
    computersx, Jun 30, 2010 IP
  3. soma56

    soma56 Guest

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the advice.
     
    soma56, Jul 18, 2010 IP