I am writing js that reads an xml file. I’m trying to add an image to my xml file. I’ve added the image to the xml file, but occassionally my entries don’t have images so what i am seeing is I get only the results that do have images.
I tried writing an if statement that checks for a null value on the variable, but i don’t know if I am doing it right or not.
Here is my JS code:
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","myfeed.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<div id='blogwrapper'>");
var x=xmlDoc.getElementsByTagName("channel");
for (i=0;i<x.length;i++)
{
document.write("<h1>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</h1><p>");
document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
document.write("<br/>");
document.write(x[i].getElementsByTagName("rights")[0].childNodes[0].nodeValue);
document.write("<br/>");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
document.write("</p>");
}
var y=xmlDoc.getElementsByTagName("item");
for (i=0;i<y.length;i++)
{
var myTitle;
var comLink;
var myDesc;
var baseImg;
myTitle = y[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
comLink = y[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
myDesc = y[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
baseImg = y[i].getElementsByTagName("imgfile")[0].childNodes[0].nodeValue;
document.write("<h2>" +myTitle+ "</h2>");
document.write(baseImg);
document.write("<p>"+myDesc+"<br/>");
document.write("<a href='"+comLink+"'>READ MORE...</a><br />");
document.write("</p>");
}
document.write("</div>");
</script>
I think what i want to do is if baseImg is null, I want to display a standard image.
Thanks for your assistance.