4

I created a window using html and javascript to display display log messages generated by my website. I want to set it up so that when the user opens the log window it auto scrolls to the bottom of the log messages. I've gotten it so that the log messages display correctly, but I am having trouble getting the auto scroll to work.

My html/javascript looks like this:

<body>
<div class="container-fluid">
  <div class="panel panel-default">
    <div class="panel-heading text-center">
      <h4 class="text-center">Log Messages</h4>
    </div>
    <div class="panel-body">
      <div class="content" id="logtext">
        <font face="courier">
          <span id="show" class='value'></span>
        </font>
      </div>
    </div>
  </div>
  <script language="javascript" type="text/javascript">
    function saveLogs(data){
      sessionStorage.setItem("logs",data.message);
    }
      $(document).ready(
        function() {
          setInterval(function() {
            Dajaxice.InterfaceApp.getLogs(saveLogs);
            var logs = sessionStorage.getItem("logs");
            document.querySelector('.content .value').innerText = logs;
            //I tried doing this and nothing happened.
            var objDiv = document.getElementById("logtext");
            objDiv.scrollTop = objDiv.scrollHeight;
          }, 1000);
        });
  </script>
</div>        
</body>

The call to dajaxice is just basically grabbing the last n lines of a log file that I have and then sending them to the js function I defined as saveLogs().

I've tried using some of the solutions given in similar questions but haven't had any luck getting them to work.

EDIT I tried setting it up like this:

   <div class="panel-body">
      <div class="content" id="logtext">
        <script language="javascript" type="text/javascript">
          document.getElementById('bottomspan').scrollIntoView();
        </script>
        <font face="courier">
          <span id="show" class='value'>
          </span>
          <div id='bottomspan'></div> 
        </font>
      </div>
    </div>
  </div>
  <script language="javascript" type="text/javascript">
    function saveLogs(data){
      sessionStorage.setItem("logs",data.message);
    }
      $(document).ready(
        // document.getElementById('bottomspan').scrollIntoView();
        function() {
          setInterval(function() {
            Dajaxice.InterfaceApp.getLogs(saveLogs);
            var logs = sessionStorage.getItem("logs");
            document.querySelector('.content .value').innerText = logs;
            var el = document.getElementById('bottomspan')
            el.scrollIntoView();
            var height = el.style.clientHeight
            window.scrollBy(0, height);
          },700);
        });
  </script>

This worked except every time the page refreshes with new messages it goes back to the bottom; this makes it hard to view older messages at the top. Is there a way to have it start at the bottom of the div but not force the user back down if they are looking at messages near the top?

1
  • Are you certain #logtext is the element that is scroll-able? Commented Aug 26, 2015 at 17:16

2 Answers 2

5

Here Try:

document.getElementById('logtext').scrollIntoView();

EDIT

document.getElementById('bottomspan').scrollIntoView();

<font face="courier">
      <span id="show" class='value'> content.... 
           <span id='bottomspan'></span>
       </span>
</font>

EDIT

var el = document.getElementById('logtext')
el.scrollIntoView();
var height = el.style.clientHeight
window.scrollBy(0, height - window.innerHeight);

Edit to answer Edit

<script language="javascript" type="text/javascript">
    function saveLogs(data){
      sessionStorage.setItem("logs",data.message);
    }
    $(document).ready(
    function() {
      setInterval(function() {
        Dajaxice.InterfaceApp.getLogs(saveLogs);
        var logs = sessionStorage.getItem("logs");
        document.querySelector('.content .value').innerText = 
      },700);

        var el = document.getElementById('bottomspan')
        el.scrollIntoView();
        var height = el.style.clientHeight
        window.scrollBy(0, height);
    });

Sign up to request clarification or add additional context in comments.

8 Comments

Its also good because it's supported by all major browsers and doesn't require any additional libraries like jQuery
I tried adding this under document.querySelector('.content .value').innerText = logs; but causes the scrolling to start at the top of the div every time the page is refreshed.
is your span above or below the content or is the content in the span?
The content is in the span, but I tried changing it to document.getElementById('show').scrollIntoView(); and it produced the same results.
put an element in the span below the content and scroll to that
|
0

Just focus on the element to make the element visible.

<span id="feed_item" />

document.getElementById("feed_item").focus();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.