I am developing a web based seating plan for my office. Below is some simple XML I have done up so I can test if my idea is working – on mouse over the desk image will turn green if desk free and red if desk taken (the image i used was sliced with Fireworks and now the rest of my dev is being done in Visual Studio 2010.
<?xml version="1.0" encoding="utf-8" ?>
<company>
<employee id="1" email="Test1@hotmail.com" phone="1234">Test 1</employee>
<employee id="2" email="Test2@hotmail.com" phone="6789">Test 2</employee>
<employee id="" email="" phone="">Free Desk</employee>
</company>
So the below is what I was trying in my Javascript (Note there is more code below/above for reading in the xml, etc. The two alerts were tests I was trying – if commented in I can see that the value 1 and 2 is appearing on the webpage so I think it is reading the XML fine.
//alert(xmlObj.childNodes(0).getAttribute("id"))
//alert(xmlObj.childNodes(1).getAttribute("id"))
for (var i = 0; i < 4; i++)
{
**if (xmlObj.childNodes(i).getAttribute("id") == 1)**
{
stringData = stringData.replace("s1.gif", "s3.gif");
}
else
{
stringData = stringData.replace("s1.gif", "s2.gif");
}
}
However when I run I get the follwoing error on the line in Bold. Microsoft JScript runtime error: ‘xmlObj.childNodes(…)’ is null or not an object. Now I’m thinking it does not like the use of i – I was hoping that for the value 1 (i.e – desk 1 it would turn red (s1.gif being replaced with s3.gif) and for other numbers desk 0 and desk 2 it would turn red. Has anyone any ideas of what I am doing wrong?
You’ve only specified 3 children for your
companyobject. Yourforloop executes4times, thus overrunning the bounds of thechildNodesobject withincompany.Change your
forloop tofor (var i = 0; i < xmlObj.childNodes.length; i++)and you won’t have any exceptions thrown.