Need HELP: Firefox/nodeValue oddity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stone Chen

    #1

    Need HELP: Firefox/nodeValue oddity

    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.fir stChild.nodeVal ue==productID) {
    some code
    }



    full code listed below:

    HTML BODY
    =============== =============== =
    <div id="writeroot"> </div>
    <script type="text/javascript">
    importXML("pric es.xml", priceTable, "kenko", "L37 Super PRO");
    </script>


    JAVASCRIPT
    =============== =============== =
    function importXML(xmlFi le, xmlDisplay, para1, para2) {
    if (window.ActiveX Object) {
    xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.onreadys tatechange = function () {
    if (xmlDoc.readySt ate == 4) xmlDisplay(para 1, para2);
    }
    } else if (document.imple mentation &&
    document.implem entation.create Document) {
    xmlDoc = document.implem entation.create Document("", "", null);
    xmlDoc.onload = function() {
    xmlDisplay(para 1, para2);
    }
    } else {
    alert('Sorry, your browser doesn't support XML scripting');
    return;
    }
    xmlDoc.async = "false";
    xmlDoc.load(xml File);
    }

    function priceTable(prod uctCat, productID) {
    var pCat = xmlDoc.getEleme ntsByTagName(pr oductCat);
    var x = pCat[0].getElementsByT agName("item");

    var newTable = document.create Element("table" );
    newTable.setAtt ribute("border" ,1);
    var tableBody = document.create Element("tbody" );
    newTable.append Child(tableBody );
    var row = document.create Element("tr");
    var emptyCell = document.create Element("th");
    row.appendChild (emptyCell);
    for (j=3; j<x[0].childNodes.len gth; j++) {
    if (x[0].childNodes[j].nodeType != 1) continue;
    var container = document.create Element("th");
    var theData = document.create TextNode(x[0].childNodes
    [j].nodeName.subst ring(1));
    container.appen dChild(theData) ;
    row.appendChild (container);
    }

    tableBody.appen dChild(row);

    for (i=0; i<x.length; i++) {
    var row = document.create Element("tr");
    // if (x[i].firstChild.fir stChild.nodeVal ue==productID) {
    if (window.ActiveX Object) {
    row.style.setAt tribute("cssTex t", "background-color:
    #cfc;");
    } else if (document.imple mentation &&
    document.implem entation.create Document) {
    row.setAttribut e("style", "background-color:
    #cfc;");
    }
    // }
    var nameCell = document.create Element("th");
    var productName = document.create TextNode(x[i].childNodes
    [1].firstChild.nod eValue);
    nameCell.append Child(productNa me);
    row.appendChild (nameCell);
    for (j=3; j<x[i].childNodes.len gth; j++) {
    if (x[i].childNodes[j].nodeType != 1) continue;
    var priceCell = document.create Element("td");
    var priceData = document.create TextNode(x[i].childNodes
    [j].firstChild==nu ll ? "" : x[i].childNodes[j].firstChild.nod eValue);
    priceCell.appen dChild(priceDat a);
    row.appendChild (priceCell);
    }
    tableBody.appen dChild(row);
    }
    document.getEle mentById("write root").appendCh ild(newTable);
    }


    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>
  • Martin Honnen

    #2
    Re: Need HELP: Firefox/nodeValue oddity



    Stone Chen wrote:
    [color=blue]
    > 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.fir stChild.nodeVal ue==productID) {
    > some code
    > }[/color]

    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.doc umentElement.fi rstChild
    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 getElementsByTa gName 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/

    Comment

    Working...