XmlHttpRequest Difference Between Firefox and Internet Explorer

If this post helps you, click an ad

The XMLHttpRequest object is initialized differently in earlier versions of Internet Explorer as compared to Firefox.

As of Internet Explorer 7, an XMLHttpRequest be can be initialized simply with:

new XMLHttpRequest();

The preceding always worked with Firefox. In earlier versions of Internet Explorer, however, the libraries differed on how to initialize XMLHttpRequest. For this reason, both of the following calls have to be attempted as well:

new ActiveXObject('Msxml2.XMLHTTP');
new ActiveXObject('Microsoft.XMLHTTP');

This should be done as a try/catch block, with the call that will work in Firefox and Internet Explorer 7 coming first as it is the most likely to succeed.

try {
  var objXHR = new XMLHttpRequest();
} catch (e) {
try {
  var objXHR = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
  var objXHR = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {
  document.write('XMLHttpRequest not supported'); }
}
}

Related Articles:
Creating an XMLHttpRequest Object for Ajax
JavaScript Try Catch Finally

2 Responses to “XmlHttpRequest Difference Between Firefox and Internet Explorer”

  1. AJAX « Sparrow132’s Blog Says:

    [...] { document.write(’XMLHttpRequest not supported’); } } } Differences of IE and Mozilla http://blog.techsaints.com/2007/09/10/xmlhttprequest-difference-between-firefox-and-internet-explore... [...]

  2. AJAX « Nreed132's Blog Says:

    [...] For a longer explanation of how different browsers interact check out this blog post. [...]

Leave a Reply