ie9中的XML Javascript未定义错误

时间:2011-08-26 08:35:07

标签: javascript jquery xml

我的网站上有一个'行话破坏者',它使用xml文件加载单词的A-Z,单击时会显示每个单词的简短简短说明。这在所有浏览器中工作正常,最新的ie是我得到的'未定义'错误。使用的jscript是

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.length; 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;
    }
}

关于我如何使用<li><a href="javascript:Jargon.showWords('f');">a</a></li>

调用jscript im

加载以f开头的所有项目的列表然后当我点击该列表中的一个项目时说“Fiduciary”它会触发javascript:Jargon.showDef('f1');然后将其加载到定义div中

然而在ie9中它显示“未定义”。它适用于所有其他浏览器

以下XML示例:

<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 个答案:

没有答案