I'm having trouble getting a portion of the site I'm developing to auto refresh every 10 seconds using AJAX. Here's what I'm doing that isn't working quite right. Your guidance is very much appreciated!
<!-- Creates the XMLHttpRequest Object for AJAX Functionality -->
function createXHR(){
var request = false;
try {
request = new ActiveXObject ('Msxml2.XMLHTTP');
}
catch (err2) {
try {
request = new ActiveXObject ('Microsoft.XMLHTTP');
}
catch (err3) {
try {
request = new XMLHttpRequest();
}
catch (err1) {
request = false;
}
}
}
return request;
}
<!-- AJAX Content Refresh Every 10 Seconds -->
function refreshContent(){
var xhr = createXHR();
if (xhr==null){
alert("It appears that your browser is a dinosaur...update it and come back.");
return;
}
var url = "refreshContent.php";
var params = "sid="+Math.random();
xhr.open ("POST", url, true);
xhr.setRequestHeader ("Content-type", "application/ x-www-form-urlencoded");
xhr.setRequestHeader ("Content-length", params.length);
xhr.setRequestHeader ("Connection", "close");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
document. getElementById ("news").innerHTML = xhr.responseText;
}
}
xhr.send (params);
setTimeout ("refreshContent()", 1000);
}
Then I've got this for the HTML onload event:
<body onload= "refreshContent()">
-------------------------
Like I said, for some reason it's not updating the contents of the page without a page refresh...HELP!
Tags: