开发者

XML Javascript undefined error in ie9

开发者 https://www.devze.com 2023-03-30 18:24 出处:网络
I have a \'jargon buster\' on my site that uses an xml file to load an A-Z of words which when clicked display a brief short explanation of each word. This works fine in all browsers bar the latest ie

I have a 'jargon buster' on my site that uses an xml file to load an A-Z of words which when clicked display a brief short explanation of each word. This works fine in all browsers bar the latest ie's which i get an 'undefined' error with. The jscript im using is below

Jargon = {
    xmlfile:    'http://www.mysite.com/jargon.xml',
    xml:        null,
    wordHolder: 'words',
    defHolder:  'definition',
    idprefix:   'jargon_',
    selected:   null,
    init:       function () {
        var con = Jargon.xhcon();
        Jargon.wordHolder = $(Jargon.wordHolder);
        Jargon.defHolder = $(Jargon.defHolder);
        if (!con || !Jargon.wordHolder || !Jargon.defHolder) {
            return;
        }

        function conComplete(oXML) {
            Jargon.xml = oXML.responseXML;
            //Jargon.showWords('a');
        }
        con.connect(Jargon.xmlfile, 'GET', Math.random(), conComplete);
    },
    showWords:  function (c) {
        if (Jargon.selected) {
            Jargon.selected.className = '';
        }
        var words = Jargon.getWords(c);
        while (Jargon.wordHolder.childNodes.length > 0) {
            Jargon.wordHolder.removeChild(Jargon.wordHolder.childNodes[0]);
        }
        while (Jargon.defHolder.childNodes.length > 0) {
            Jargon.defHolder.removeChild(Jargon.defHolder.childNodes[0]);
        }
        for (var i = 0; i < words.length; i++) {
            var o = document.createElement('a');
            o.href = 'javascript:Jargon.showDef(\'' + words[i].id + '\');';
            o.id = Jargon.idprefix + words[i].id;
            //o.onclick = Jargon.showDef;
            o.appendChild($t(words[i].name));
            Jargon.wordHolder.appendChild(o);
            Jargon.wordHolder.appendChild(document.createElement('br'));
        }
        if (!words.length) {
            var o = document.createElement('p');
            var s = 'There are no words for the letter ' + c.toUpperCase();
            Jargon.wordHolder.appendChild(o.appendChild($t(s)));
        } 
    },

    showDef: function (id) {
        var o = $(Jargon.idprefix + id);

        if (Jargon.selected) {
            Jargon.selected.className = '';
        }

        if (o) {
            o.className = 'selected';
            Jargon.selected = o;
        }

        var defobjs = Jargon.getDef(id);

        while (Jargon.defHolder.childNodes.length > 0) {
            Jargon.defHolder.removeChild(Jargon.defHolder.childNodes[0]);
        }

        var heading = document.createElement('span');
        heading.className = "jargtitle";
        heading.appendChild(document.createTextNode(defobjs[1][0].textContent));

        Jargon.defHolder.appendChild(heading);

        var definition = document.createElement('span');
        definition.className = "jargdefinition";

        definition.appendChild(document.createTextNode(defobjs[0][0].textContent));

        Jargon.defHolder.appendChild(definition);
    },

    getWords: function(c) {
        var x = Jargon.xml;
        var letters = x.getElementsByTagName('letter');
        var oLetter = null;
        for (var i = 0; i < letters.length; i++) {
            if (letters[i].getAttribute('id') == c) {
                oLetter = letters[i];
                break;
            }
        }
        if (!oLetter) {
            return [];
        }

        var words = [];
        for (i = 0; i < oLetter.childNodes.l开发者_StackOverflow中文版ength; i++) {
            var oJargon = oLetter.childNodes[i];
            if (oJargon.nodeName == 'jargon') {
                var s = Jargon.getName(oJargon);
                words[words.length] = {
                    id: oLetter.childNodes[i].getAttribute('id'),
                    name: s
                };
            }
        }
        return words;
    },
    getDef:     function (id) {
        var x = Jargon.xml;
        var j = null;
        var temp = new Array(2);
        var jargons = x.getElementsByTagName('jargon');
        for (var i = 0; i < jargons.length; i++) {
            if (jargons[i].getAttribute('id') == id) {
                j = jargons[i];
                break;
            }
        }
        if (!j) {
            return [];
        }

        //return [];

        for (i = 0; i < j.childNodes.length; i++) {
            if (j.childNodes[i].nodeName == 'name') {
                temp[1] = j.childNodes[i].childNodes;
            }
        }
        for (i = 0; i < j.childNodes.length; i++) {
            if (j.childNodes[i].nodeName == 'desc') {
                temp[0] = j.childNodes[i].childNodes;
            }
        }
        //return [];
        return temp;
    },
    cloneNode:  function (oldNode, deep) {
        deep = (deep) ? true : false;
        // a replacement to the normal dom clone node
        // this will copy xml nodes to html nodes
        // which can then be inserted into the document
        // scope in all browsers
        // See for for the bug http://www.quirksmode.org/blog/archives/2005/12/xmlhttp_notes_c.html
        var newNode = null;
        if (oldNode.nodeType == '3') {
            // textnode
            newNode = $t(oldNode.nodeValue);
        }
        else if (oldNode.nodeType == '1') {
            // element node
            newNode = document.createElement(oldNode.nodeName);
            if (deep) {
                for (var i = 0; i < oldNode.childNodes.length; i++) {
                    newNode.appendChild(Jargon.cloneNode(oldNode.childNodes[i], true));
                }
            }
        }
        return newNode;
    },
    getName:    function (oJargon) {
        for (var i = 0; i < oJargon.childNodes.length; i++) {
            if (oJargon.childNodes[i].nodeName == 'name') {
                var oName = oJargon.childNodes[i];
                var s = '';
                for (var j = 0; j < oName.childNodes.length; j++) {
                    if (oName.childNodes[j].nodeType == 3) {
                        // text node
                        s += oName.childNodes[j].nodeValue;
                    }
                }
                return s;
            }
        }
        return '';
    },
    xhcon:      function () {
        var xmlhttp, bComplete = false;
        try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) { try { xmlhttp = new XMLHttpRequest(); }
        catch (e) { xmlhttp = false; }}}
        if (!xmlhttp) {
            return null;
        }
        this.connect = function(sURL, sMethod, sVars, fnDone) {
            if (!xmlhttp) {
                return false;
            }
            bComplete = false;
            sMethod = sMethod.toUpperCase();

            try {
                if (sMethod == "GET") {
                    xmlhttp.open(sMethod, sURL+"?"+sVars, true);
                    sVars = "";
                }
                else {
                    xmlhttp.open(sMethod, sURL, true);
                    xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
                    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && !bComplete) {
                        bComplete = true;
                        fnDone(xmlhttp);
                    }
                };
                xmlhttp.send(sVars);
            }
            catch(z) { return false; }
            return true;
        };
        return this;
    }
}

In terms of how im calling the jscript im using <li><a href="javascript:Jargon.showWords('f');">a</a></li>

which loads a list of all items that begin with f then when i click one of items from that list say "Fiduciary" it triggers javascript:Jargon.showDef('f1'); which in turns loads the and into a definition div

however in ie9 it displays "undefined" . It works in all other browers

Example of the XML below:

<letter id="f"> -<jargon id="f1"> <name>Fiduciary</name> <desc>in a position of trust. This includes people such as trustees looking after trust assets for the beneficiaries and company directors running a company for the shareholders' benefit.</desc> </jargon> -<jargon id="f2"> <name>Forfeiture</name> <desc>the loss of possession of a property because the tenancy conditions have not been met by the tenant.</desc> </jargon> -<jargon id="f3"> <name>Freehold</name> <desc>describing land that only the owner has any rights over.</desc> </jargon> -<jargon id="f4"> <name>Free of encumbrances</name> <desc>no one else having any rights over something. When property is owned by someone and nobody else has any rights over it, it is owned free of encumbrances.</desc> </jargon> </letter>
0

精彩评论

暂无评论...
验证码 换一张
取 消