tomfmason
09-26-2006, 01:29 AM
I am not sure exactly how to phrase this but I will try anyway.
Ok I have written a Ajax support chat app. and I am running into an issue. I will give a real basic example of what I mean..
Example chat:
Name: Message:
Tom test
Tom test2
Tom test3
Tom test4
Tom test5
OK now lets say the the 5th message fills the chat box. When I enter a new message I have to scroll down to see the next message. This continues throughout the chat session.
I guess what my question is... How can I focus the chat box to the last message.
Here is a the section of the process.php that deals with the retrieval of the chat text.
case "getChat":
$sql = "SELECT * FROM `chat_text` ORDER BY `text_id` ASC";
$res = mysql_query($sql);
$param = "";
if (mysql_num_rows($res) == 0) {
echo "Ready to chat.";
}else{
while ($rw = mysql_fetch_assoc($res)) {
$param .= '<b>' . $rw['name'] . '</b>: ' . $rw['text'] . '<br />';
}
}
//I added this because so that I maybe able to focus on this id
$param .= '<div id="end"></div>';
echo $param;
break;
now here is the js function that gets the chat
function getChat() {
getText.open('get', 'process.php?action=getChat');
getText.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
getText.send(null);
getText.onreadystatechange = handleGetChat;
}
function handleGetChat() {
if (getText.readystate == 4) {
var chatText = getText.responseText;
document.getElementById('chatBox').innerHTML = chatText;
chatTimer = setTimeout('getChat();', 2000);
}
}
Any suggestions would be great.
Thanks,
Tom
Ok I have written a Ajax support chat app. and I am running into an issue. I will give a real basic example of what I mean..
Example chat:
Name: Message:
Tom test
Tom test2
Tom test3
Tom test4
Tom test5
OK now lets say the the 5th message fills the chat box. When I enter a new message I have to scroll down to see the next message. This continues throughout the chat session.
I guess what my question is... How can I focus the chat box to the last message.
Here is a the section of the process.php that deals with the retrieval of the chat text.
case "getChat":
$sql = "SELECT * FROM `chat_text` ORDER BY `text_id` ASC";
$res = mysql_query($sql);
$param = "";
if (mysql_num_rows($res) == 0) {
echo "Ready to chat.";
}else{
while ($rw = mysql_fetch_assoc($res)) {
$param .= '<b>' . $rw['name'] . '</b>: ' . $rw['text'] . '<br />';
}
}
//I added this because so that I maybe able to focus on this id
$param .= '<div id="end"></div>';
echo $param;
break;
now here is the js function that gets the chat
function getChat() {
getText.open('get', 'process.php?action=getChat');
getText.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
getText.send(null);
getText.onreadystatechange = handleGetChat;
}
function handleGetChat() {
if (getText.readystate == 4) {
var chatText = getText.responseText;
document.getElementById('chatBox').innerHTML = chatText;
chatTimer = setTimeout('getChat();', 2000);
}
}
Any suggestions would be great.
Thanks,
Tom