Wednesday, 27 April 2011

Javascript

As I want to use javascript to load up xml files of the information for my website I needed to find some code which enabled me to do this and modify it.

I managed to find this website with a good example of how to do it and modified it to work with my website

http://www.codeproject.com/KB/scripting/xmldso.aspx

Here is the code for the site:

<script type="text/javascript">
//<![CDATA[
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","XML/Weapons.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("HK416");
for (i=0;i<x.length;i++)
 {
 document.write("<h1>HK416<\/h1>");
 document.write("<p /><img src=images/HK416.jpg img id=imgstyle img width=30% img height=30%/>");
 document.write("<p /><b>Name: <\/b>");
 document.write(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue);
 document.write("<br /><b>Manufacturer: <\/b>");
 document.write(x[i].getElementsByTagName("MANUFACTURER")[0].childNodes[0].nodeValue);
 document.write("<br /><b>Weight: <\/b>");
 document.write(x[i].getElementsByTagName("WEIGHT")[0].childNodes[0].nodeValue);
 document.write("<br /><b>Length: <\/b>");
 document.write(x[i].getElementsByTagName("LENGTH")[0].childNodes[0].nodeValue);
 document.write("<br /><b>Cartridge: <\/b>");
 document.write(x[i].getElementsByTagName("CARTRIDGE")[0].childNodes[0].nodeValue);
 document.write("<br /><b>Fire rate: <\/b>");
 document.write(x[i].getElementsByTagName("FIRERATE")[0].childNodes[0].nodeValue);
 document.write("<br /><b>Magazine size: <\/b>");
 document.write(x[i].getElementsByTagName("MAGAZINESIZE")[0].childNodes[0].nodeValue);
 }
document.write("<\/table>");
//]]>
</script>

No comments:

Post a Comment