Java Reference
In-Depth Information
in this block, the loop iterates to the next element in the array. If no version is found on the computer, then
the function returns null, like this:
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
}
}
return null;
}
Now you don't have to worry about ActiveX objects to create an XMLHttp object. If you call this func-
tion, it'll do all the work for you.
var oHttp = createXmlHttpRequest();
Calling the Native Constructor: The Other Browsers
IE 7+, Firefox, Opera, Safari, and Chrome boast a native implementation of the XMLHttpRequest object;
it is an object located in the window object. Creating an XMLHttpRequest object is as simple as calling its
constructor.
var oHttp = new XMLHttpRequest();
This line creates an XMLHttpRequest object, which you can use to connect to, and request and receive
data from, a server. Unlike the ActiveX object in the previous section, XMLHttpRequest does not have
different versions. Simply calling the constructor creates a ready to use XMLHttpRequest object.
Playing Together: One Function to Create them All
Just as with all other cross-browser issues, a solution can be found to create an XMLHttpRequest object
for all browsers. You already wrote the createXmlHttpRequest() function, so expand it to provide
cross-browser functionality.
function createXmlHttpRequest()
{
if (window.XMLHttpRequest)
Search WWH ::




Custom Search