By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

Need HELP: Firefox/nodeValue oddity

P: n/a
Hello,

When I try to insert this line of code into my script, Firefox refuses to
render. Could someone explain to me why that is?

if (x[i].firstChild.firstChild.nodeValue==productID) {
some code
}

full code listed below:

HTML BODY
===============================
<div id="writeroot"></div>
<script type="text/javascript">
importXML("prices.xml", priceTable, "kenko", "L37 Super PRO");
</script>
JAVASCRIPT
===============================
function importXML(xmlFile, xmlDisplay, para1, para2) {
if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) xmlDisplay(para1, para2);
}
} else if (document.implementation &&
document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = function() {
xmlDisplay(para1, para2);
}
} else {
alert('Sorry, your browser doesn't support XML scripting');
return;
}
xmlDoc.async = "false";
xmlDoc.load(xmlFile);
}

function priceTable(productCat, productID) {
var pCat = xmlDoc.getElementsByTagName(productCat);
var x = pCat[0].getElementsByTagName("item");

var newTable = document.createElement("table");
newTable.setAttribute("border",1);
var tableBody = document.createElement("tbody");
newTable.appendChild(tableBody);
var row = document.createElement("tr");
var emptyCell = document.createElement("th");
row.appendChild(emptyCell);
for (j=3; j<x[0].childNodes.length; j++) {
if (x[0].childNodes[j].nodeType != 1) continue;
var container = document.createElement("th");
var theData = document.createTextNode(x[0].childNodes
[j].nodeName.substring(1));
container.appendChild(theData);
row.appendChild(container);
}

tableBody.appendChild(row);

for (i=0; i<x.length; i++) {
var row = document.createElement("tr");
// if (x[i].firstChild.firstChild.nodeValue==productID) {
if (window.ActiveXObject) {
row.style.setAttribute("cssText", "background-color:
#cfc;");
} else if (document.implementation &&
document.implementation.createDocument) {
row.setAttribute("style", "background-color:
#cfc;");
}
// }
var nameCell = document.createElement("th");
var productName = document.createTextNode(x[i].childNodes
[1].firstChild.nodeValue);
nameCell.appendChild(productName);
row.appendChild(nameCell);
for (j=3; j<x[i].childNodes.length; j++) {
if (x[i].childNodes[j].nodeType != 1) continue;
var priceCell = document.createElement("td");
var priceData = document.createTextNode(x[i].childNodes
[j].firstChild==null ? "" : x[i].childNodes[j].firstChild.nodeValue);
priceCell.appendChild(priceData);
row.appendChild(priceCell);
}
tableBody.appendChild(row);
}
document.getElementById("writeroot").appendChild(n ewTable);
}
XML
====================
<products>
<lens>
<uv>
<item>
<en>kenko</en>
<gb>L37</gb>
<description>
</description>
<d49mm>132</d49mm>
<d52mm>143</d52mm>
<d55mm>160</d55mm>
<d58mm>170</d58mm>
<d62mm>210</d62mm>
<d67mm>245</d67mm>
<d72mm>285</d72mm>
<d77mm>320</d77mm>
<d82mm>425</d82mm>
<d86mm>715</d86mm>
</item>
</uv>
</lens>
</products>
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


Stone Chen wrote:
When I try to insert this line of code into my script, Firefox refuses to
render. Could someone explain to me why that is?

if (x[i].firstChild.firstChild.nodeValue==productID) {
some code
}


It is not clear what you are trying to achieve but using childNodes
respectively firstChild and lastChild and expecting a certain kind of
node when you deal with the DOM is prone to errors, DOM implementation
have different ways of dealing with whitespace text nodes in the DOM,
some (Mozilla HTML and XML DOM) include them always, some (IE HTML DOM)
ignore the always, some (MSXML XML DOM) have settings on whether to
ignore them. Thus if you have
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>
then with some implementations
xmlDocument.documentElement.firstChild
is a text node with white space, with others it is the <god>Kibo</god>
element node.
Therefore don't navigate firstChild/lastChild/childNodes without
checking the nodeType or better avoid accessing child node if element
nodes are all what you are looking for then use getElementsByTagName or
make sure you walk childNodes node by node until you find the right
nodeType (1 for element nodes, 3 for text nodes).

If you have further problems then try to make a reduced test case and
post a URL but tell us exactly what the Firefox JavaScript console says.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.