views:

273

answers:

3

Hi,

I have been having problems getting my xml to load in all browsers. I have finally got it working in safari but now it is not working in IE. Has anyone got any solutions?

Thanks

function displayResult() {
    var xmlRequest = new XMLHttpRequest();
    xmlRequest.open("GET", document.getElementById('tracklisting').innerHTML, false);
    xmlRequest.send(null);
    xml= xmlRequest.responseXML;

    var xslRequest = new XMLHttpRequest();
    xslRequest.open("GET", document.getElementById('xslt').innerHTML, false);
    xslRequest.send(null);
    xsl = xslRequest.responseXML;

    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    document.getElementById("example").appendChild(resultDocument);
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(displayResult);
A: 

Are you using IE6 by any chance? if so the native call

new XMLHttpRequest();

will not work... see IE Blog for details

Dror
A: 

I now have it working in Safari, FF and IE7 but IE6 is still elluding me!!

enter code here

var xslRequest = new XMLHttpRequest(); xslRequest.open("GET", document.getElementById('xslt').innerHTML, false); xslRequest.send(null); xsl = xslRequest.responseXML;

if (window.ActiveXObject) { ex=xml.transformNode(xsl); document.getElementById("example").innerHTML=ex; } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xsltProcessor=new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); resultDocument = xsltProcessor.transformToFragment(xml,document); document.getElementById("example").appendChild(resultDocument); } }

You should use the if (window.ActiveXObject) before the xslRequest = new XMLHttpRequest(); section otherwise you have an exception and the rest of the code does not execute
Dror
A: 

I have finally figured it all out so if anyone needs the code here it is

function loadXMLDoc(fname)

{ var xmlDoc; // code for IE if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } xmlDoc.async=false; xmlDoc.load(fname); return(xmlDoc); }

function displayResult() {

if (window.ActiveXObject) { xml=loadXMLDoc(document.getElementById('tracklisting').innerHTML); xsl=loadXMLDoc(document.getElementById('xslt').innerHTML); }

else if (document.implementation && document.implementation.createDocument)
{ var xmlRequest = new XMLHttpRequest(); xmlRequest.open("GET", document.getElementById('tracklisting').innerHTML, false); xmlRequest.send(null); xml= xmlRequest.responseXML;

var xslRequest = new XMLHttpRequest(); xslRequest.open("GET", document.getElementById('xslt').innerHTML, false); xslRequest.send(null); xsl = xslRequest.responseXML;

}

if (window.ActiveXObject) { ex=xml.transformNode(xsl); document.getElementById("example").innerHTML=ex; xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

} // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xsltProcessor=new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); resultDocument = xsltProcessor.transformToFragment(xml,document); document.getElementById("example").appendChild(resultDocument); } }

function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } }

addLoadEvent(displayResult);