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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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). Quote Link to comment 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> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 Then did you try my solution? Quote Link to comment 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.