...

View Full Version : Ajax function works in IE7, Google Chrome & Safari but not in FF3



JUD
09-13-2008, 10:10 PM
I've come up with this script to return responseText or responseXML depending on which function I call.



AJAX = {
req: false,
res: '',

requireText: function(url){
if(window.XMLHttpRequest){
req = new XMLHttpRequest;
}else if(window.ActiveXObject){
try{
req = new ActiveXObject("MSXML2.XMLHTTP");
}catch(e){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}else{
alert("Your browser doesn't support AJAX");
return;
}

if(req){
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
res = req.responseText;
}
}
req.open("GET", url, false);
req.send(null);
}

return res;
},

requireXml: function(url){
if(window.XMLHttpRequest){
req = new XMLHttpRequest;
}else if(window.ActiveXObject){
try{
req = new ActiveXObject("MSXML2.XMLHTTP");
}catch(e){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}else{
alert("Your browser doesn't support AJAX");
return;
}

if(req){
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
res = req.responseXML;
}
}
req.open("GET", url, false);
req.send(null);
}

return res;
}
}


I call the functions like this:



var textRes = AJAX.requireText("path/to/myFile");


and



var xmlRes = AJAX.requireXml("path/to/myFile");


Everything is working fine in IE7, Chrome & Safari but FF3(FireBug) gives me an error:



res is not defined


I've been trying different things for hours but I can't get it to work.

Any help would be greatly appreciated.

JUD
09-13-2008, 11:57 PM
Never mind. After searching Google for a bit I've finally figured it out.

Turns out I was using the onreadystatechange handler when I didn't need to be as I'm opening the XmlHttpRequest in synchronous mode.

Everythings working fine now.
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum