?? ajax_chat.js
字號:
var sendReq = createAjaxObj();
var receiveReq = createAjaxObj();
var lastMessage = 0;
var mTimer;
var chatId;
var userId;
//Function for initializating the page.
function startChat() {
//Set the focus to the Message Box.
document.getElementById('txt_message').focus();
//Start Recieving Messages.
initial();
getChatText();
}
function initial()
{
chatId = document.frmmain.cid.value;
userId = document.frmmain.uid.value;
}
//Gets the browser specific XmlHttpRequest Object
function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest)
{ // ???Mozilla, Safari ????
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType)
httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject)
{ // ???IE
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}
//Gets the current messages from the server
function getChatText() {
//判斷上次請求的狀態是否完成或是還未發送請求
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
//發送獲得消息的服務器地址
var submitURL = "getMessage?chatId="+chatId+"&last=" + lastMessage;
receiveReq.open("GET", submitURL, true);//建立請求
receiveReq.onreadystatechange = handleReceiveChat; //當請求狀態改變時調用handleReceiveChat方法
receiveReq.send(null);//發送請求
}
}
//Add a message to the chat server.
function sendChatText() {
//如果輸入的消息為空,提示用戶輸入
if(document.getElementById('txt_message').value == '') {
alert("You have not entered a message");
return;
}
//判斷上次發送消息請求的狀態是否完成或是還未發送請求
if (sendReq.readyState == 4 || sendReq.readyState == 0) {、
//保存消息的服務器地址
var submitURL = "sendMessage?cid="+chatId+"&uid="+userId+"&msg="+document.getElementById('txt_message').value;
sendReq.open("POST", submitURL , true);//建立請求連接
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange = handleSendChat; //當請求狀態改變時調用handleSendChat方法
sendReq.send(null);//發送請求
document.getElementById('txt_message').value = '';//設置提交消息文本框為空
}
}
//When our message has been sent, update our page.
function handleSendChat() {
//取消間隔獲取數據的方法
clearInterval(mTimer);
//重新獲得調用getChatText()方法
getChatText();
}
//Function for handling the return of chat text
function handleReceiveChat() {
if (receiveReq.readyState == 4) {//此時請求已經完成
//獲得顯示消息的圖曾層元素
var chat_div = document.getElementById('div_chat');
//獲得返回后的XML文件
var xmldoc = receiveReq.responseXML;
//得到所有的新的消息記錄
var message_nodes = xmldoc.getElementsByTagName("message");
var n_messages = message_nodes.length;
//循環每一條新消息,組織成HTML格式數據
for (i = 0; i < n_messages; i++) {
var user_node = message_nodes[i].getElementsByTagName("user");
var text_node = message_nodes[i].getElementsByTagName("text");
var time_node = message_nodes[i].getElementsByTagName("time");
chat_div.innerHTML += '(<font class="chatUser">'+user_node[0].firstChild.nodeValue + '</font>) said at ';
chat_div.innerHTML += '<font class="chatTime">' + time_node[0].firstChild.nodeValue + '</font><br />';
chat_div.innerHTML += '<span class="title">'+text_node[0].firstChild.nodeValue + '</span><br />';
chat_div.scrollTop = chat_div.scrollHeight;
//每循環一條消息,記錄到全局變量lastMessage中
lastMessage = (message_nodes[i].getAttribute('id'));
}
//獲取數據后,設置2秒鐘后再次調用getChatText()方法
mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
}
}
//This functions handles when the user presses enter. Instead of submitting the form, we
//send a new message to the server and return false.
function blockSubmit() {
sendChatText();
return false;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -