Phil (flameswithin) wrote in ajaxengine,

Help initializing AJAX.

Hey everybody, I've got a quick question. I'm just starting to work with AJAX on my website, and I've just about got it working. I ran into a curious problem though, and I'm hoping you all could help me.

I've been initializing my XMLHttpRequest object this way:

//try for Mozilla browsers, Safari, etc first:
if (window.XMLHttpRequest) {
   httpRequest = new XMLHttpRequest();
// Try for IE second.
else if (window.ActiveXObject) {
   try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}

Everything was all hunky dory until I noticed Internet Explorer returning error messages because it was trying to run [other-browser] specific code. I realized that the first half of my 'if' statement (window.XMLHttpRequest) was working in IE!

I tried reversing the order of my if-else statement, so that IE would run the window.ActiveXObject code first, only to discover that won't initialize an XMLHttpRequest object in IE at all.

So, my question is, what should I do here? Is it kosher these days to initialize an XMLHttpRequest object by var foo = new XmlHttpRequest(); in all browsers?

I appreciate any help you can offer, and if I need to clarify anything, I'll be glad to. Have a good one!
  • Post a new comment


    default userpic