otester Posted July 27, 2010 Share Posted July 27, 2010 Currently got chat box done using SQL and PHP, however when text overflows the scroll bar won't stay at the bottom.. Currently using this code (within header): <script language="text/javascript"> var objDiv = document.getElementById("site_chat_box"); objDiv.scrollTop = objDiv.scrollHeight; </script> Unfortunately it doesn't work Any ideas? Thanks, otester Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/ Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 Setting the scrollTop attribute initially won't do any good. You'll have to adjust the scrollTop attribute every time that chat is appended to the div. Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091759 Share on other sites More sharing options...
otester Posted July 27, 2010 Author Share Posted July 27, 2010 Setting the scrollTop attribute initially won't do any good. You'll have to adjust the scrollTop attribute every time that chat is appended to the div. So is there anyway to achieve what I want to do? I've been hunting around the net and it currently isn't looking very good for me Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091764 Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 Just find the part in your script where new messages are added to the div and right after it place that code to update the scrollTop. Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091768 Share on other sites More sharing options...
otester Posted July 27, 2010 Author Share Posted July 27, 2010 Just find the part in your script where new messages are added to the div and right after it place that code to update the scrollTop. Ok I changed the code between the header to: <script language="text/javascript"> var objDiv = document.getElementById("site_chat_box"); </script> I have a file which deals with the database and relaying the message to the div, I added this after the php tags: <script language="text/javascript"> objDiv.scrollTop = objDiv.scrollHeight; </script> Still doesn't work :/ Btw I am using overflow-x and overflow-y properties (CSS3). Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091778 Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 Are the messages appended using JavaScript or are the messages loaded when the pages loads? If the latter then you can do something like this: <script type="text/javascript"> window.onload = init; function init() { var objDiv = document.getElementById("testid"); objDiv.scrollTop = objDiv.scrollHeight; } </script> Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091791 Share on other sites More sharing options...
otester Posted July 27, 2010 Author Share Posted July 27, 2010 Are the messages appended using JavaScript or are the messages loaded when the pages loads? If the latter then you can do something like this: <script type="text/javascript"> window.onload = init; function init() { var objDiv = document.getElementById("testid"); objDiv.scrollTop = objDiv.scrollHeight; } </script> The messages are loaded by fetching them from an SQL database, no JavaScript involved in the process, so when the page loads. Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091821 Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 Then did you try my solution? Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091830 Share on other sites More sharing options...
otester Posted July 27, 2010 Author Share Posted July 27, 2010 Then did you try my solution? Works perfectly, thank you very much for your help! (For anyone else that reads this: AlexWD's solution went between header tags and no extra JavaScript code was needed). Link to comment https://forums.phpfreaks.com/topic/209024-scrollbar-position/#findComment-1091836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.