AJAX初级聊天室代码(2)


var nowNum = "m"
function getId(objId){
    return document.getElementById(objId)
}
function getName(objName){
    return document.getElementsByName(objName)[0]
}
var xmldoc
function createxmldoc(){
    if(window.XMLHttpRequest){
        xmldoc = new XMLHttpRequest();
        if(xmldoc.overrideMimeType){
                xmldoc.overrideMimeType("text/xml");
        }
    }else if(window.ActiveXObject){
        try{
            xmldoc = new ActiveXObject("Msxml4.XMLHTTP");
        }catch(e){
            try{
                xmldoc = new ActiveXObject("Msxml3.XMLHTTP");
            }catch(e){
                try{
                    xmldoc = new ActiveXObject("Msxml2.XMLHTTP");
                }catch(e){
                    try{
                        xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
                    }catch(e){}
                }
            }
        }
    }
    if(!xmldoc){
        return false;
    }
}
function createQueryString(names){
    var conts = getName(names).value
    if(conts != ""){
        if(conts == "clear"){
            getId("chatmain").innerHTML = ""
            clean()
            focs()
            return false
        }else{
            return conts
        }
    }else{
        return false
    }
}
function doUsingGet(){
    xmldoc = false
    createxmldoc()
    var url = "chat.asp?clien="+nowNum+"&timeStamp="+new Date().getTime();
    xmldoc.open("GET",url,true);
    xmldoc.onreadystatechange = CheckState;
    xmldoc.setRequestHeader("Content-Type","text/xml;charset=GB2312;");
    xmldoc.send(null);
}
function doUsingPost(){
    createxmldoc()
    if(createQueryString("inputs")){
        if(createQueryString("named")){
            var named = "&named="+createQueryString("named")
        }else{
            var named = ""    
        }
        if(createQueryString("inputs") == "cls"){
            nowNum = "m"
        }
        var url = "chat.asp?clien="+nowNum+"&cont="+createQueryString("inputs")+named+"&timeStamp="+new Date().getTime();
        xmldoc.open("GET",url,true);
        xmldoc.onreadystatechange = CheckState;
        xmldoc.setRequestHeader("Content-Type","text/xml;charset=GB2312;");
        xmldoc.send(null);
        getId("errors").innerHTML = ""
        addChat(createQueryString("inputs"));
        clean()
        focs()
    }else{
        getId("loadifo").innerHTML = "请输入对话内容!"    
        focs()
    }
}
function CheckState(){
    if(xmldoc.readyState == 1){
        //getId("loadifo").innerHTML = "连接服务器"
        }
    else if(xmldoc.readyState == 2){
        //getId("loadifo").innerHTML = "开始加载数据"
        }
    else if(xmldoc.readyState == 3){
        //getId("loadifo").innerHTML = "正在加载数据"
        }
    else if(xmldoc.readyState == 4){
        if(xmldoc.status == 200){
            results()
        }
        else{
            getId("loadifo").innerHTML = "错误"+xmldoc.status;
            focs()
        }
    }
}
function results(){
    resXml()
}
function resXml(){
    var res = xmldoc.responseXML.documentElement;
    var resLen = res.getElementsByTagName("items").length
    if(getTag(res,0,"num") != nowNum){
        //alert(xmldoc.responseXML.xml)
        nowNum = getTag(res,0,"num")
        for(var i=0; i<resLen; i++){
            var useName = getTag(res,i,"name")
            var useMag = getTag(res,i,"conts")
            var useIp = getTag(res,i,"ip")
            if(nowNum != "0" && useMag != ""){
                if(useName != ""){
                    var lis = "用户"+ useName + "说:" + useMag + " ["+ useIp + "]"
                }else{
                    var lis = "用户 ["+ useIp + "] 说:" + useMag
                }
            }else{
                var lis = "用户 ["+ useIp + "] 进入聊天室!"
            }
            //var newp = document.createElement("p")
            //newp.innerHTML = lis
            //getId("chatmain").appendChild(newp)
            getId("chatmain").innerHTML += lis + "<br />"
        }
        innerSize()
    }
}
function getTag(response,i,objTagName){
    try{
        var nodeV = response.getElementsByTagName(objTagName)[i].firstChild.nodeValue;
    }catch(e){
        var nodeV = ""
    }
    return nodeV;
}
var overs
function innerSize(){
    if(overs != 1){
        if(getId("chatmain").offsetHeight < getId("outmain").offsetHeight){
            getId("chatmain").style.marginTop = getId("outmain").offsetHeight - getId("chatmain").offsetHeight +"px";
        }else{
            getId("chatmain").scrollIntoView(false)    
        }
    }
}
window.onload = function(){
    getId("outs").onmousedown = function(){
        overs = 1
    }
    getId("outs").onmouseout = function(){
        overs = 0
        focs()
    }
    getName("inputs").onkeydown = function(e){
        if(document.all){
            var Keys = event.keyCode;    
        }else{
            var Keys = e.which;
        }
        //alert(Keys)
        if(Keys == "38"){
            preChat()
        }else if(Keys == "40"){
            nextChat()
        }else if(event.ctrlKey && Keys == "13"){
            doUsingPost()
        }else if(event.ctrlKey && Keys == "46"){
            clean()
        }
    }

}
setInterval("doUsingGet()",1000)



JS辅助文件:请保存为 images/display.js

程序代码

程序代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wfzjgj.html