PDA

View Full Version : Chat AJAX



stephenalistoun
01-05-2009, 07:17 PM
Hi all,

I can not see the messages when other people enter in text but when I enter the text i eventually see the messages. What could I be doing wrong in my Javascript Code:




<script type="text/javascript">

function ajaxRequest()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}


function init()
{
loadURL("checkUpdate.php");

clearInterval(mTimer);

mTimer = setTimeout("init()",1000);
}


function loadURL(url)
{
mygetrequest=new this.ajaxRequest()
mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4 || mygetrequest.readyState=="complete")
{
processData(mygetrequest.responseText); //download successful
}
}

mygetrequest.open("GET", url, true);
mygetrequest.send(null);
}

function processData(responseText)
{
var countValue = responseText;

if(countValue > 0)
{
showChatData("getcd.php");
}
}




function showChatData(url)
{
var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()
mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4 || mygetrequest.readyState=="complete")
{
showData(mygetrequest.responseText); //download successful
}
}

mygetrequest.open("GET", url, true);
mygetrequest.send(null);
}



function showData(responseText)
{
document.getElementById("txtHint").innerHTML = responseText; /*mygetrequest.responseText;*/

var objDiv = document.getElementById("leftHand");

objDiv.scrollTop = objDiv.scrollHeight;

}

function checkEnter(e)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
}

if(keynum == 13)
{
insertData();
}

}


function insertData()
{
str = document.getElementById("chatText").value;

if(str == "")
{
alert("Please enter in Text");
}

else
{
font = document.getElementById("fontStyle").value;

var url = "insertRow.php?value=" + str +"&font=" + font;

var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()

mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4)
{
if (mygetrequest.status == 200)
{
init();
}
else
{
alert("status is " + mygetrequest.status);
}
}
}
mygetrequest.open("GET", url + bustcacheparameter, true);
mygetrequest.send(null);

document.getElementById("chatText").value = "";

clearInterval(mTimer);

init();
}
}
</script>

Thanks Stephen