Im trying to create a HTML table with links from a XML file using javascript. The html tbale works fine but when it comes to the link creation it seems the links only have the first word of my string in the link. IF i do an alert(String) command i get the full string but as soon as i try to make an HTML LINK it only adds the first word.
Example:
Base link: http://kat.ph/usearch/{SEARCH FIELD GOES HERE}
String output from XML List : “Hello Sir”
Actual Output as link (without quotes): “http://kat.ph/usearch/Hello”
Expected output as link (without quotes): “http://kat.ph/usearch/Hello Sir”
Code is below
<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","playlist.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("void");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("string")[0].childNodes[0].nodeValue);
document.write("</td><td>");
//alert(x[i].getElementsByTagName("string")[0].childNodes[0].nodeValue); just testing to see what i get here
// document.write("<a href=http://www.kat.ph/usearch/" + x[i].getElementsByTagName("string")[0].childNodes[0].nodeValue + ">"+"KAT.PH"+"</a>");
document.write("<a href='" + "http://www.kat.ph/usearch/" + x[i].getElementsByTagName("string")[0].childNodes[0].nodeValue + "'>" + "KAT.PH" + "</a>");
document.write("</td></tr>");
}
document.write("</table>");
</script>
you cant have spaces in the URI (http://www.w3schools.com/jsref/jsref_encodeuri.asp). What you can do is encode it: