17
September

Essential AJAX Functions

Okay here are a couple of really usefull javascript functions when it come to ajax. Though having recently decoverd jquery I may use a little less now:

This one is cross browser and will retrive and xmldoc from a given url:


function loadXMLDoc(url) {
req = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
if(req) {
req.open("GET", url, false);
req.send("");
}
return req.responseXML
}

This will send a doucment to a url and return a textstring responce:


function sendXMLDoc(url,doc) {
req = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
if(req) {
req.open("POST", url, false);
req.send(doc);
}
return req.responseText
}

Note: I can’t remember where these came from I don’t remember writing them.
So, if you think you did and you would like me to credit or remove please let me know.