On my webpage the sql results are returned in a div where the maximum width and height has been defined and the overflow has been set to scroll. When someone visits the page I would like the default scroll position to be at the bottom. I have done a lot of research and the best solution I could find without using jQuery fails to work (Scroll to bottom of div?). Therefore I am wondering if anybody can explain where my error is or an alternate solution without using jQuery.
My Code
<html>
<style>
#test{
max-height: 150px;
max-width: 200px;
overflow: scroll;
background: #50a8ff;
}
</style>
<head>
<script language="javascript" type="text/javascript">
var objDiv = document.getElementById("test");
objDiv.scrollTop = objDiv.scrollHeight;
</script>
</head>
<body>
<div id="test"><h4>BIG TEXT</h4><h4>BIG TEXT</h4><h4>BIG TEXT</h4><h4>BIG TEXT</h4>
<h4>BIG TEXT</h4><h4>BIG TEXT</h4><h4>BIG TEXT</h4></div>
</body>
</html>
This is a simplified version of my code to demonstrate the problem.
Potential Causes
As the SQL data is fetched once the page has loaded, therefore would the scroll height be set before the div is filled, would setting a delay solve this problem? Although in this simple example I have the same problem even though the data is not being loaded from an SQL database.