LeviD77
LeviD77

Reputation: 25

document.write stops working when no data

document.write(x[i].getElementsByTagName("property")[0].childNodes[0].nodeValue);

When it gets no value, it stops working. It's a for loop:

var x=xmlDoc.getElementsByTagName("object");
for (i=0;i<x.length;i++)
  { 
  document.write("<table border='1'>");
  document.write("<tr><td>Last Communication</td><td>");
  document.write(x[i].getElementsByTagName("property")[0].childNodes[0].nodeValue);

What should I do?

Upvotes: 0

Views: 65

Answers (1)

Roman M
Roman M

Reputation: 578

try this:

var x=xmlDoc.getElementsByTagName("object");
for (i=0;i<x.length;i++) {
    document.write("<table border='1'>");
    document.write("<tr><td>Last Communication</td><td>");
    var item = x[i];
    if(item !== undefined) {
        var itemElements = item.getElementsByTagName("property");
        if(itemElements.length > 0) {
            if(itemElements[0].childNodes.length > 0) {
                document.write(itemElements[0].childNodes[0].nodeValue || '');
            }
        }
    }
  //...
}

Upvotes: 1

Related Questions