Hy all,
I am using a code that goes through an XML file and show me the data.. That code works great in IE, Firefox and Opera.. Now I would like to know how to configure it to work on Chrome..
Using the navigator.appName == "Microsoft Internet Explorer" and navigator.appName == "Netscape", I was capable of testing the identity of the browser used, and whether to use ActiveX objects or httpRequest.
Keeping in mind, when alerting the navigator.appName , on Chrome, Firefox and Opera, I get Netscape.
This is the full version of my code:
<html>
<body>
<script type="text/javascript">
alert(navigator.appName);
if (navigator.appName == "Microsoft Internet Explorer") {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
xhttp.open("GET", "http://www.multimediaprof.com/test/emp2.xml", false);
}
else if (navigator.appName == "Netscape") {
xhttp = new XMLHttpRequest();
alert("step 1");
xhttp.open("GET", "emp2.xml", false);
}
alert("step 2");
xhttp.send("");
alert("step 3");
xmlDoc = xhttp.responseXML;
alert("step 4");
alert(xmlDoc);
document.write(xmlDoc.documentElement.nodeName + " loaded");
alert("step 5");
var str = xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
alert("step 6");
alert(str);
</script>
</body>
</html>
New full version of code suggested now :
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var xhttp, xmlDoc, str;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
xhttp.open("GET", "emp2.xml", false);
} else if (window.ActiveXObject) {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
xhttp.open("GET", "http://www.multimediaprof.com/test/emp2.xml", false);
} else {
alert("Cannot create XmlHttpRequest object");
}
if (xhttp) {
xhttp.send("");
if (xhttp.responseXML != null) {
xmlDoc = xhttp.responseXML;
str = xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
alert(str);
} else {
alert("Server response was invalid.");
}
}
</script>
</head>
</html>