0

Hello everybody, I read this forum sometimes, but this is my first post :)
I have a simple function for an intranet site that should make and return a XMLHttpRequest object in most browsers (including IE8):

function createXMLHttpRequest() {
  var xmlhttp = false;
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      xmlhttp = false;
    }
   }
  } return xmlhttp;
};

Then I use it autocomplete a input text with a list of client company names from a database. Then the user selects a company and it reloads the page with that company's info. (The ajax input text box remains so the user can type in another company name)

In firefox this works fine. However, in IE8 the very first time the page loads it works fine, but after the user selects a company and the page reloads, the ajax longer works. It gives an error at "xmlhttp = new XMLHttpRequest();" saying: "Message: Object doesn't support this property or method"

If I type anything in the input, it gives the error: "Message: 'searchReq.readyState' is null or not an object" leading me to believe that somehow after the first time the page loads, IE is not able to process a XMLHttpRequest which seems unlikely, so I'm not sure where to go with my code. I've tried a bunch of other similar createXMLHttpRequest() functions, but they all have the same problem of working once then not working after.

Any ideas? Thanks in advance!

1
Contributor
1
Reply
3
Views
7 Years
Discussion Span
Last Post by newbies1
0

I'm not sure if I figured out precisely what the problem was, but I did figure out a solution. I was going to recode it in jQuery and noticed that the code i was looking at checks for ActiveX before XMLHttpRequest, so I just flipped my code to also check for ActiveX first and now it works for both IE8 and FF.

Seems like the problem is that IE8 isn't able to create a XMLHttpRequest after I reload the page (I have no idea why) so letting it create an ActiveXObject instead makes it work all the time.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.