29 September 2007

Ajax Chat Tutorial (from changathi.com)

This is the javascript used in changathi.com for simple but powerfull chat using ajax

//check it

function getXmlHttp()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest();
else if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else
{
alert("Your browser does not support AJAX!");
return null;
}
}


var isPending=false;
function GetChatUpdate()
{

if(isPending == true)
return;
isPending=true;
var xmlHttpGet= getXmlHttp();
if(xmlHttpGet == null)return;
xmlHttpGet.onreadystatechange=function()
{
if(xmlHttpGet.readyState==4 && xmlHttpGet.status==200)
{
var currentChat;
currentChat =xmlHttpGet.responseText;
document.getElementById("ChatLogDiv").innerHTML=currentChat ;
isPending=false;
}
}
xmlHttpGet.open("POST","GetChatLog.ashx",true);
xmlHttpGet.send("hellooo");
}


var isSendPending=false;
function SetChatUpdate()
{
if(isSendPending) return;
isSendPending=true;
var xmlHttpPost= getXmlHttp();
if(xmlHttpPost == null)return;
xmlHttpPost.open("POST","SetChatLog.ashx",true);
xmlHttpPost.onreadystatechange=function(){isSendPending=false;return;}
var postText=document.getElementById("TextBox1");
xmlHttpPost.send(postText.value);
postText.value="";
return false;
}

function ClearChatUpdate()
{
var xmlHttpClear= getXmlHttp();
if(xmlHttpClear == null)return;
xmlHttpClear.open("GET","ClearChatLog.ashx?id=" + Date(),true);
xmlHttpClear.send(null);
}