views:

73

answers:

1

My ajax script only works in Firefox and not ie why??

========================================

   <html>
    <head>

<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};

function dochange(src) {

var req = Inint_AJAX();
req.onreadystatechange = function () {


 if (req.readyState==4) {
      if (req.status==200) {


document.getElementById(src).innerHTML=req.responseText; //retuen value
setTimeout("dochange('showResult')",5000);
}

if (!req.responseXML.documentElement && req.responseStream)
  req.responseXML.load(req.responseStream);
 }
};
req.open("GET", "ajax.php"); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
req.send(null); //send value
}

window.onLoad=dochange('showResult');
</script>
</head>


<body>
<div id="showResult"></div> <!–page result will be displayed here–>
</body>
</html>
+4  A: 

Luke, use the Source! Better yet, use a proven JS library, e.g. jQuery. They deal with all of this cr*p and they do it better than you would ever have the time to do yourself.

Update:

Alex -- all kidding aside. The reason we are all telling you to use jQuery (or one of the other major Javascript libraries) is because getting this stuff to work on all (or even close to all) browsers is genuinely hard. As mangled as MS's handling of HTML and CSS is (and believe me, it sucks) their various implementations of JavaScript run the gamut from crap to huge-steaming-pile-of-crap. The various JS libraries give you standard, cross-browser ways of doing things -- stuff that it has taken real pros years to get right. You really, really don't want to waste your time on this stuff.

So, use a good library, get it working in Firefox using Firebug as your debugger, and you have a pretty good chance it will work in IE.

Peter Rowell
Where i'm wrong??
Alex
@Chacha102 - I think he was going for "Luke, use the force!"
karim79
help me please..
Alex
Are you next going to tell me his last name is not Skywalker? That these are not the droids we are looking for? By God! I will not tolerate these young whippersnappers who have no respect for a barely applicable, 30+ year old quote from the only Star Wars movie worth watching. In my day we walked -- ***walked*** -- to the theater! Up hill! Both ways! Now all of you kids get off my lawn!
Peter Rowell
Alex, You are wrong because you are trying to do something that has already been done, and has been made much easier for you. Go to jquery.com and download a copy. It has excellent documentation and you should solve your problem easily.
Chacha102