HTML + CSS + JS + XML issue with null values

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.

after you define baseImg try the following

if(baseImg === null)
{
   baseImg = 'CODE FOR YOUR STANDARD IMAGE';
}

Then leave the rest as it is. That should be okay.

i actually figured a different way. Since i have control over the xml feed and its dynamically created, I just added if else statement to the actual xml code. But, i won’t always have control over the output of the xml so this is good to know.

Why do you need 3 equal marks? I think i had tried but i was only using 2 equal marks. haha

So, I just tried it again and i think i understand now why this doesnt work. So, my xml is parsed dynamically. In some cases the xml node called <imgfile> (which becomes my javascript var baseImg) doesn’t exist because i am pulling from 2 blog sources (using Expression Engine). So, if the node imgfile doesn’t exist, would baseImg get passed a null value? The answer seems that if it doesn’t get a value then the script stops running. I should get back 37 results but instead i get back 5 because the script stops running when it hits the first blog entry that doesn’t have a <imgfile> node at all. SO, the answer is i need to modify my xml file so that imgfile node always exists even if there is no image.

ok thanks for helping me think through this.