Jump to content

ajax function required some feedback for improvement?


sayedsohail

Recommended Posts

Hi everyone,

 

The function below is written by matt, i would like to see if phpfreeks could incorporate this inside the basic ajax example listed under this section.

 

Morever, i would appreciate if someone suggest any improvements to this function for a real life application.

 

Thanks

 

var xmlreqs = new Array();

function CXMLReq(freed) {
this.freed = freed;
this.xmlhttp = false;
if (window.XMLHttpRequest) {
	this.xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}

function xmlreqGET(url) {
var pos = -1;
for (var i=0; i<xmlreqs.length; i++) {
	if (xmlreqs[i].freed == 1) { pos = i; break; }
}
if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
if (xmlreqs[pos].xmlhttp) {
	xmlreqs[pos].freed = 0;
	xmlreqs[pos].xmlhttp.open("GET",url,true);
	xmlreqs[pos].xmlhttp.onreadystatechange = function() {
		if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
	}
	if (window.XMLHttpRequest) {
		xmlreqs[pos].xmlhttp.send(null);
	} else if (window.ActiveXObject) {
		xmlreqs[pos].xmlhttp.send();
	}
}
}


function xmlhttpChange(pos) {
if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
	if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
		handle_response(xmlreqs[pos].xmlhttp.responseXML);
	} else {
		handle_error();
	}
	xmlreqs[pos].freed = 1;
}
}

Reference: http://www.drakware.com

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.