AJAX progress bar

With few lines of JavaScript and CSS you can make simple AJAX progress bar. JavaScript will periodically ask for progress value and server will respond with XML. Progress value should be extracted from the XML and displayed as width of the DIV element. To start progress bar in this example, please click on the Start button and wait for a second to begin. If you have installed Firebug, open Net tab and watch how this page sends request to the Web server.

More precisely, every 1500ms send_request() function sends request to the server (see setInterval line - button actions). ajax-progress-bar.php returns XML and request_handler() processes received XML. Progress value from XML has meaning of percentage completion of a job.

In the following JavaScript source, please focus to the sending and handling requests. Initialization of the XMLHttpRequest is written in the cross-browser manner and there's nothing more to add. The onreadystatechange property is a function that receives the feedback. It is important to note that the feedback function must be assigned before each send, because upon request completion the onreadystatechange property is reset. This is evident in the Mozilla and Firefox source.

var progress;   // progress element reference
var request;    // request object
var intervalID; // interval ID

// button actions
function polling_start() {intervalID = window.setInterval('send_request()',1500)}
function polling_stop()  {window.clearInterval(intervalID)}

// define reference to the progress bar and create request object
window.onload = function (){
	progress = document.getElementById('progress');
	request  = initXMLHttpClient();
}

// create an XMLHttpClient in a cross-browser manner
function initXMLHttpClient(){
	var xmlhttp;
	try {xmlhttp=new XMLHttpRequest()} // Mozilla/Safari/IE7 (normal browsers)
	catch(e){ 												 // IE (?!)
		var success=false;
		var XMLHTTP_IDS=new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0',
															'MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
		for (var i=0; i<XMLHTTP_IDS.length && !success; i++)
			try {success=true; xmlhttp=new ActiveXObject(XMLHTTP_IDS[i])}	catch(e){}
		if (!success) throw new Error('Unable to create XMLHttpRequest!');
	}
	return xmlhttp;
}

// send request to the server
function send_request(){
	request.open('GET','/my/ajax-progress-bar.php', true); // open asynchronus request
	request.onreadystatechange = request_handler;          // set request handler
	request.send(null);                                    // send request
}

// request handler
function request_handler(){
	if (request.readyState == 4){ // if state = 4 (the operation is completed)
		if (request.status == 200){ // and the HTTP status is OK
			// get progress from the XML node and set progress bar width and innerHTML
			var level=request.responseXML.getElementsByTagName('PROGRESS')[0].firstChild;
			progress.style.width = progress.innerHTML = level.nodeValue + '%';
		}
		else{ // if request status is different then 200
			progress.style.width = '100%';
			progress.innerHTML='Error: ['+request.status+'] '+request.statusText;
		}
	}
}

Here you can see the source of ajax-progress-bar.php (how is job progress emulated for this example). In your case you will have to calculate percentage of completion and return it as the XML document.

<?php
header('Content-type: text/xml'); // define XML content type
print '<?xml version="1.0"?>';    // print XML header
?>
<DOCUMENT><PROGRESS><?= mktime() % 100 ?></PROGRESS></DOCUMENT>

Related posts

Bookmark and Share

29 Responses to “AJAX progress bar”

  1. The above was supposed to be a simple for loop from zero to 1000 but it got mangled.

  2. Seeing the actual code in your /my/ajax-progress-bar.php would complete this example. Otherwise it's left to the imagination as to how your process produces that XML doc.

  3. dbunic says:

    @roger - Last listing in post is actually the source of the ajax-progress-bar.php used in this demonstration. It hasn't much sense, but it good enough for demo. ;)

  4. I'll try that.
    I learned a lot from your demo.
    Thanks again.

  5. smia says:

    could you use this for an upload progress bar? if so, what parameters do I change?

  6. Bugsy says:

    Great tutorial :) It couldn't be explained easyer!

  7. Siddhesh says:

    Nice plugin without using any framework!!!

  8. Punit says:

    Can you tell me how can I implement the php part in C. I am developing an Ajax based file upload progress bar and if i use text file to update data at server end, then progress bar is not updated as expected as Ajax is not able to read the updated values when the file is being updated. Any idea how to implement this in C based CGI.

  9. 123doing says:

    It's very good.
    I like this.
    Thanks for share.
    And I wrote something to introduce this project for my readers.
    You can find the post about this in my website.
    If something is wrong,pls figure it out.thanks.

Leave a Reply