Hey DW fellas
I'm having some trouble when I run my code on IE. I tried it on Chrome and Firefox and it works, but it doesn't at IE. I think that is a Javascript problem, but I can't find the solution. So please, if you can give me a clue of the problem and how to resolve it I'll be so greatful.
Basiclly I need to know if some function on this code is non compatible with IE.
Variables' names are in spanish, because that's my native language :P
See ya!

var conexion1=crearXMLHttpRequest();
/*
    This code brings the mysql query results and insert them into a div called "prods"
*/
function vercont_prods(nro) 
{
  var url='';
  url='ver.php?id='+nro;
  conexion1.onreadystatechange = procesarEventos;
  conexion1.open("GET",url,true);
  conexion1.send(null);
}

function procesarEventos()
{
  if(conexion1.readyState == 4)
  {
    var cont_prods = document.getElementById("prods");
    cont_prods.innerHTML='';
    cont_prods.innerHTML = conexion1.responseText;
    cont_prods.focus();
  } 
  else 
  {
    cont_prods.innerHTML = 'Loading...';
  }
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

Dani AI

Generated

correctly flagged the XMLHttpRequest factory as a likely culprit and was right to recommend stepping through the code with IE's developer tools. A working factory is necessary but not always sufficient — a few small, cross‑browser issues commonly bite IE and are worth checking even after the factory is fixed.

The main practical problems to watch for in this thread are (1) using a single global XHR instance, which can create race conditions, (2) accessing DOM variables that may be undefined in certain readyState paths, and (3) assuming focus or a successful HTTP status. The original procesarEventos reads cont_prods only when readyState == 4 but then tries to use it in the else branch; that leaves cont_prods undefined and will throw when the else branch executes. Also, calling .focus() on a non‑focusable element can behave differently in IE.

A compact, robust pattern:

function procesarEventos() {
  var cont_prods = document.getElementById('prods');
  if (!cont_prods) return;
  if (this.readyState === 4) {
    if (this.status === 200 || (location.protocol === 'file:' && this.status === 0)) {
      cont_prods.innerHTML = this.responseText;
      if (typeof cont_prods.focus === 'function') cont_prods.focus();
    } else {
      cont_prods.innerHTML = 'Error: ' + this.status;
    }
  } else {
    cont_prods.innerHTML = 'Loading...';
  }
}

Additional practical tips: create a new XHR for each request instead of reusing a global object; append a cache‑buster (e.g. &_= + Date.now()) to GET URLs to avoid IE caching; check the status code (and allow status 0 for local files); and use F12 to inspect console and network traffic. These small changes cover most IE-specific XHR failures and complement the factory fix that implemented.

Recommended Answers

All 4 Replies

Do you know where it breaks? Debug with the developer tools, or put in alert()'s to find out more.

Probably, you may have XMLHTTPRequest problem. Are you sure crearXMLHttpRequest() works on IE and you get new XMLHTTPRequest ? Should try what said.

Zero13 thank you so much! You were right, createXMLHttpRequest() was wrong, it doesn't work properly. My code now is
`

function crearXMLHttpRequest() 
            {
                var xmlhttp;
                if (window.XMLHttpRequest)
                {
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
              return xmlhttp;
            }`

Mark as Solved then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.