samedi 21 octobre 2017

Javascript check 'URL' is empty then populate with "" , else display url as hyperlink

I have some javascript that displays titles from an XML.

When the title is clicked the information associated with it will pop up. I am having trouble when URL is empty. If it is empty I don't want the URL displayed. It should just look like URL.

If it is present I want the url displayed as a hyperlink.

I am new to this so please take it easy on me!

Code is below

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "memos.xml", false);
xmlhttp.send();
xmlDocument = xmlhttp.responseXML;
memos = xmlDocument.getElementsByTagName("memo");

table = "<tr><th>Title</th></tr>";
for (count = 0; count < memos.length; count++) {
  table += "<tr onclick='displayTitle(" + count + ")'><td>";
  table += memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue;
  table += "</td>";
}
document.getElementById("demo").innerHTML = table;

function displayTitle(count) {

  document.getElementById("showTitle").innerHTML =
    "ID: " +
    memos[count].getAttribute("id") +
    "<br>Title: " +
    memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue +
    "<br>Sender: " +
    memos[count].getElementsByTagName("sender")[0].childNodes[0].nodeValue +
    "<br>Recipient: " +
    memos[count].getElementsByTagName("recipient")[0].childNodes[0].nodeValue +
    "<br>Date: " +
    memos[count].getElementsByTagName("date")[0].childNodes[0].nodeValue +
    "<br>Message: " +
    memos[count].getElementsByTagName("message")[0].childNodes[0].nodeValue +
    "<br>Url: " +
    urlNode = memos[count].getElementsByTagName("url")[0];
  if (urlNode.hasChildNodes()) {
    url = urlNode.childNodes[0].nodeValue;
  } else {
    url = "";
  }

}

Aucun commentaire:

Enregistrer un commentaire