Hello I have built a chat system, using html and JavaScript (ajax).
There are a lot of messages loaded in the chat and it goes down the page.
So I tried to devise a way for the chat messages to be scrolled down onload of the chat page using the function below.
<script>
  function scrolldown() {
    var box = document.getElementById('chatmessagecontainer');
    box.scrollTop = box.scrollHeight;
  }
</script>
but onload method never fires this function,
<body onload="scrolldown()">
However, I noticed that onclick of a button the function fires, like below
<input type="button" id="sub" onclick="scrolldown()" name="click" value="click" />
Messages are loaded into the chat container by the below function in this id "#display_info".
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
  setInterval(loaddata, 1000);
  function loaddata() {
    var msgid = document.getElementById('msgid');
    if (msgid) {
      $.ajax({
        type: 'post',
        url: 'load.php',
        data: {
          msgid: msgid.value,
        },
        success: function(response) {
          // We get the element having id of display_info and put the response inside it
          $('#display_info').html(response);
        },
      });
    } else {
      $('#display_info').html('Please Enter Some Words');
    }
  }
  </script>
Why doesn't the onload method run my function?
 
     
    