Java Reference
In-Depth Information
You want to use the latest version possible when creating an XmlHttpRequest object as it contains bug
fi xes and enhanced performance. The downside is that not everyone will have the same version installed
on their computer. However, you can write a function to use the latest version of XmlHttp installed on
the user's computer.
With the previous version information, write a function called createXmlHttpRequest() to create an
XMLHttpRequest object with the latest version supported by the user's computer.
function createXmlHttpRequest()
{
var versions =
[
“MSXML2.XmlHttp.6.0”,
“MSXML2.XmlHttp.3.0”
];
//more code here
}
This code defi nes the createXmlHttpRequest() function. Inside it, an array called versions contains
the different version names recommended by Microsoft. Notice that the version names are listed starting
with the newest fi rst. This is done because you always want to check for the newest version fi rst and
continue with the next newest version until you fi nd the version installed on the computer.
To decide what version to use, use a for loop to iterate through the elements in the array and then
attempt to create an XMLHttpRequest object.
function createXmlHttpRequest()
{
var versions =
[
“MSXML2.XmlHttp.6.0”,
“MSXML2.XmlHttp.3.0”
];
for (var i = 0; i < versions.length; i++)
{
try
{
var oHttp = new ActiveXObject(versions[i]);
return oHttp;
}
catch (error)
{
//do nothing here
}
}
//more code here
}
An error is thrown if a specifi c version isn't installed on the user's computer. Therefore, use a try...
catch block inside the loop to catch the error; this is the only way to determine if a version is installed on
the computer. Code execution drops to the catch block if a version doesn't exist. Since nothing happens
Search WWH ::




Custom Search