var http_request = false;
// All major browser except IE will pass this line.
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// This line will cause an error in FireFox if the page called is not valid XML
// If you see 'Not well formed' error check your XML request.
http_request.overrideMimeType('text/xml');
}
// This line of code is especially for IE
} else if (window.ActiveXObject) { // IE
// There is also a slightly different way that IE invokes XMLHttpRequest Object.
// And depending on the version of IE the implementation can be slightly different.
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
// Make the function call that will wait for and validate the response.
http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
function alertContents(http_request) {
// The 'ready.state' of 4 means the request is completed.
if (http_request.readyState == 4) {
// A server response of 200 means everything is OK.
if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert('There was a problem with the request.');
}
}