PDA

View Full Version : Ajax progress bar help



kkroo
10-14-2006, 08:03 PM
Hey,
I have an upload progress bar, it is currently updating using META-REFRESH, that isnt that great, so I am attempting to convert it to AJAX and having it fetch every second. I am currently having a problem fetching the data from the cgi file and outputting it. If somebody could look this code over for errors, that would be great.

this is just part of my code, this is the part that isn't working...


function makeRequest()
{

var fields = new Array()
fields[0] = "status"
fields[1] = "est"
fields[2] = "elapsed"
fields[3] = "percent"

for(i=0;i<fields.length;i++)
{
var http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request)
{
alert ("Your Browser does not support AJAX, you will be redirected to the non ajax progress bar.")
window.location = "http://yahoo.com"
return
}
var url="progress.cgi"
// url=url+"?mode=ajax"
// url=url+"&get="+fields[i]
http_request.onreadystatechange = function() { update_html( http_request, fields[i] ); };
http_request.open('GET', url, true);
http_request.send(null);


}
}


function update_html(http_request, field)
{
if (http_request.readyState == 4)
{
//alert(field)
if (http_request.status == 200)
{
if ( field == "percent" )
{
var table = document.getElementById("percent");
table.setAttribute("bgcolor", "red");
table.setAttribute("width", http_request.responseText);
if ( http_request.responseText == "100%" )
{
stopclock()
}
}
else
{
//alert(field)
document.getElementById(field).innerHTML=http_request.responseText
}
}
else
{
alert('There was a problem with the request.');
}
}
}