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.
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.