Long story short: I want for a rss feed by the UN to show on my sidebar after I click to open it. Javascript & rss.xml are external files Desired result: Clicking rss button displays news on the side bar. Issues from the console :
XHRGEThttp://localhost/Dynamic static content Test/rss.xml
Uncaught TypeError: request.responseXML is null
showFeed http://localhost/Dynamic static content Test/Javascript.js:18
xmlLoad http://localhost/Dynamic static content Test/Javascript.js:12
onload http://localhost/Dynamic static content Test/:1
Javascript:
var docname = "rss.xml"
var doctype = "unkown"
function xmlLoad(){
try{
if(window.ActiveXObject){
request= new ActiveXObject("Microsoft.XMLHTTP");
} else{
request = new window.XMLHttpRequest();
}
request.open("GET",docname,true);
request.send(null);
request.onreadystatechange = showFeed;
} catch (exc){
alert ("An error has ocurred" exc.message);
}
}
function showFeed(){
xmlDoc = request.responseXML.documentElement;
var maxitems = 3
var feedBody = ""
var titlelist = xmlDoc.getElementsByTagname("title");
var linklist = xmlDoc.getElementsByTagname("link");
var browsername = navigator;
for (i = 0; i<maxitems; i ){
feedBody = feedBody "<a href='" linklist[i].firstChild.nodeValue "'>" titlelist[i]
}
document.getElementById("info").innerHTML = "Browser: " browsername "Doc type: " document.getElementById("feedarea").innerHTML== feedBody;
}```
CodePudding user response:
You call showFeed
every time the ready state changes (when there is a readystatechange
event), and you have no checks inside it to make sure that either:
- The state has changed to "Done"
- There wasn't an error
Use the load
event instead.