I am trying to use ajax to display an rss feed. It works fine in IE, but does not work in firefox. Researching, I have heard that firefox has an error that occurs on an asynchronous ajax call. Does anyone know a work around for firefox. Any help much appreciated.

<body>

<script language="javascript" type="text/javascript">

	var xmlHttp;
		try
  			{    			
  			xmlHttp=new XMLHttpRequest();			
  			}
		catch (e)			
  			{  
  			 try
    			{    
    			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	
    			}
  			catch (e)
    			{    
    			try
      				{      
      				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
      				}
    			catch (e)
      				{      
      				alert("No AJAX support!");       		     
      				}    
      			}  
      		}
	
     
	xmlHttp.onreadystatechange=function()
    	{     
    	if(xmlHttp.readyState==4)
      		{
      		var rss_response=xmlHttp.responseText;
      		
			try 
	  			{
	  			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  			xmlDoc.async="false";
	  			xmlDoc.loadXML(rss_response);								
	  			}
			catch(e)
	  			{
	  			try 
	  				{
	  				parser=new DOMParser();
	  				xmlDoc=parser.parseFromString(rss_response,"text/xml");	
								
	  				}
	  				catch(e)
	  					{
	  					alert(e.message);  					
	  					}
				}	
				
				
				for (loop=1; loop<=10; loop++)
					{
					document.write("<p><a href=\"" + xmlDoc.getElementsByTagName("link")[loop].childNodes[0].nodeValue + "\">")
					document.write(xmlDoc.getElementsByTagName("title")[loop].childNodes[0].nodeValue)
					document.writeln("</a><br>");					
					document.writeln(xmlDoc.getElementsByTagName("description")[loop].childNodes[0].nodeValue + "</p>");					
					}	
      	
      		}
    	}
    var url = "http://www.kbtx.com/home/headlines/index.rss"
  	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);    
  	

</script>

</body>

Thanks,

Gil

It's not a Firefox bug its that you're only coding for IE. Use this to get your request object

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

Then just

var xmlHttp = createXMLHTTPObject();

Thanks for your answer. However, I've run some tests and it does create the request object in firefox.

This statement creates the firefox obj: xmlHttp=new XMLHttpRequest();

The problem is on the readystate function call. Firefox will not call the function.

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.