[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

XML Javascript doesn't work in Safari - Please help.

New to XML in XHTML. Use it in ActionScript successfully all the time - but this is driving me nuts.

This code (below) works everywhere but Safari... Go ahead and cut and paste it and you'll see.

I can't find a solution in English. Been programming for 20 years, but I don't speak Computer Science - so looking for how this code needs to be updated to include the Safari DOM. I've read that the solution is a form of HTTPrequest somethine - but can't find any actual solution. Just a few notes of people bragging that they 'figured it out' - but had to rearrange their code to do it.

What is missing?


<html>
<body>
<script type="text/javascript">
	var xmlDoc=null;
	if (window.ActiveXObject){// code for IE
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}else if (document.implementation.createDocument){// code for Mozilla, Firefox, Opera, etc.
		xmlDoc=document.implementation.createDocument("","",null);
	}else{
		alert('Your browser cannot handle this script');
	}
	
	if (xmlDoc!=null){
		xmlDoc.async=false;
		xmlDoc.load("http://www.echo-x.com/portfolio.xml");
 
		var x=xmlDoc.getElementsByTagName("ART");
		for (i=0;i<x.length;i++){
			document.write("<img border='0' src='http://www.echo-x.com/images/navbuttons/grid_"+x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue + ".gif'>");
		}
	}
</script>
</body>
</html>

Open in new window

0
LazarusBlack
Asked:
LazarusBlack
  • 2
1 Solution
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
0
 
jwmcpeakCommented:
I forget what exactly the problem, but Safari either didn't support createDocument() or did not support the creation of an XML DOM. But indeed, the way around the issue was using XMLHttpRequest. Try the code below:
function createHttp() {
    if (window.XMLHttpRequest) {
        var oHttp = new XMLHttpRequest();
        return oHttp;
    } else if (window.ActiveXObject) {
        var versions = [
            "MSXML2.XmlHttp.6.0",
            "MSXML2.XmlHttp.3.0"
        ];
 
        for (var i = 0; i < versions.length; i++) {
            try {
                var oHttp = new ActiveXObject(versions[i]);
                return oHttp;
            } catch (error) {
              //do nothing here
            }
        }
    }
 
    throw new Error("Your browser doesn't support XMLHttpRequest");
}
 
function req_readystatechange() {
    if (req.readyState == 4) {
        if (req.status == 200 || req.status == 304) {
            getImages(req.responseXML);
        }
    }
}
 
function getImages(dom) {
    var x = dom.getElementsByTagName("ART");
    for (var i=0;i<x.length;i++){
        document.write("<img border='0' src='http://www.echo-x.com/images/navbuttons/grid_"+x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue + ".gif'>");
     }
}
 
var req = createHttp();
req.onreadystatechange = req_readystatechange;
 
req.open("GET", "http://www.echo-x.com/portfolio.xml", true);
req.send(null);

Open in new window

0
 
LazarusBlackAuthor Commented:
I tried both offered solutions and the Sarissa files didn't work:

http://www.echo-x.com/xmltest1.html

However, jwmcpeak's solution appears to work perfectly. THANKS!!!!!

I'm going to go back to Sarissa and try to figure out why that didn't work - but if you have a suggestion, please refer to the code below.


<html>
<head>
<script type="text/javascript" src="http://www.echo-x.com/scripts/sarissa/sarissa.js"> </script>
</head>
<body>
<script type="text/javascript">
	var xmlDoc = Sarissa.getDomDocument("http://www.echo-x.com/portfolio.xml","foo", null);  
	
	if (xmlDoc!=null){
		xmlDoc.async=false;
		xmlDoc.load("http://www.echo-x.com/portfolio.xml");
 
		var x=xmlDoc.getElementsByTagName("ART");
		for (i=0;i<x.length;i++){
			document.write("<img border='0' src='http://www.echo-x.com/images/navbuttons/grid_"+x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue + ".gif'>");
		}
	}
</script>
</body>
</html>

Open in new window

0
 
jwmcpeakCommented:
I don't know what your XML schema looks like, but I assume you're not using a namespace. So try calling the getDomDocument() factory method without passing any parameters:
var xmlDoc = Sarissa.getDomDocument();
 
if (xmlDoc != null) {
    xmlDoc.async=false;
    xmlDoc.load("http://www.echo-x.com/portfolio.xml");
 
    var x = xmlDoc.getElementsByTagName("ART");
    for (i=0;i<x.length;i++){
        document.write("<img border='0' src='http://www.echo-x.com/images/navbuttons/grid_"+x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue + ".gif'>");
    }
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now