if (typeof XMLHttpRequest != "undefined")
The above line will return true for Firfox and Safari so we can create an object. For IE we will use ActiveXObject to create the object. Depending on the type of ActiveXObject support which depends on version of IE we will create the object. If in all fail in crating the object then we will return error message.
function developXMLHttp() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
var aVersions = [ "MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp","Microsoft.XMLHttp"
];
for (var x = 0; x < aVersions.length; x++) {
try {
var p2XmlHttp = new ActiveXObject(aVersions[x]);
return p2XmlHttp;
} catch (oError) {
//Do nothing
}
}
}
throw new Error("Error..XMLHttp object could be created.");
}
To use this function we will create an object like this.
function my_form(){
var myxml=developXMLHttp();
document.write(typeof(myxml));
}
window.onload=my_form();
Here is the full code. Save this page test.html and open in browser.
<html>
<head>
<title>(Type a title for your page here)</title>
</head>
<body >
<script>
function developXMLHttp() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
var aVersions = [ "MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp","Microsoft.XMLHttp"
];
for (var x = 0; x < aVersions.length; x++) {
try {
var p2XmlHttp = new ActiveXObject(aVersions[x]);
return p2XmlHttp;
} catch (oError) {
//Do nothing
}
}
}
throw new Error("Error..XMLHttp object could be created.");
}
function my_form(){
var myxml=developXMLHttp();
document.write(typeof(myxml)+' This is the type of the object');
}
window.onload=my_form();
</script>
</body>
</html>
GET method of Form submission using Ajax xmlhttp Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.