I have this code, to load chat
function getMessages(letter) {
var div = $("#messages");
$.get('msg_show.php', function(data) {
div.html(data);
});
}
setInterval(getMessages, 100);
What I have to add, to automatically scroll down #messages div on page load, and again with every new chat post?
This, doesnt work:
function getMessages(letter) {
var div = $("#messages");
$.get('msg_show.php', function(data) {
div.html(data);
$('#messages').scrollTop($('#messages')[0].scrollHeight);
});
}
setInterval(getMessages, 100);
Dont have to mix jquery and javascript. Use like this,
Put the
scrollTop()
insideget()
method.Also you missed a parameter in the
getMessage
method call..if you just scrollheight it will make a problem when user will want to see his previous message. so you need to make something that when new message come only then the code. use jquery latest version. 1.here I checked the height before message loaded. 2. again check the new height. 3. if the height is different only that time it will scroll otherwise it will not scroll. 4. not in the if condition you can put any ringtone or any other feature that you need. that will play when new message will come. thanks
its worked for me try this, let me know if any doubts,
I found out this very simple method while experimenting: set the
scrollTo
to the height of the div.As I am not master in js but I myself write code which check if user is at bottom. If user is at bottom then chat page will automatically scroll with new message. and if user scroll up then page will not auto scroll to bottom..
JS code -
html code -
after append in JQuery,just add this line