tinfanide Posted October 14, 2011 Share Posted October 14, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script>function parseXML(method,xml,async){if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); xmlhttp.open(method,xml,async); xmlhttp.send(); }else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") ; xmlhttp.async=async; xmlhttp.load(xml); }xmlDoc = xmlhttp.responseXML;}parseXML("GET","abc.xml",false);var xmlDoc; function loadxml() { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.onreadystatechange = show; xmlDoc.load("abc.xml"); }function show(){if(xmlDoc.readyState==4)document.getElementById("image").src = xmlDoc.getElementsByTagName("pic")[0].attributes.getNamedItem("path").nodeValue;}window.onload = loadxml;</script></head><body><img id="image" src="" /></body></html> <?xml version="1.0" encoding="utf-8"?><gallery><pic path="Joey 009.JPG" desc="1" /><pic path="Joey 010.JPG" desc="2" /><pic path="Joey 011.JPG" desc="" /><pic path="Joey 012.JPG" desc="" /><pic path="Joey 016.JPG" desc="" /><pic path="Joey 017.JPG" desc="" /><pic path="Joey 018.JPG" desc="" /><pic path="Joey 019.JPG" desc="" /></gallery> How can I write one function that works for both IE and FF?I think there's something wrong with the getAttribute in XML parsing in IE (works in FF)but now I contain two functions and both IE and FF cannot load the XML file. Link to comment Share on other sites More sharing options...
Ingolme Posted October 14, 2011 Share Posted October 14, 2011 I might be wrong, but I was quite sure getAttribute works well in Internet Explorer. What error do you get in your code? Link to comment Share on other sites More sharing options...
tinfanide Posted October 14, 2011 Author Share Posted October 14, 2011 xmlhttp.open(method,xml,async); IE9 Develop Tool reports: Access is denied. lightboxImage.setAttribute("src",xmlDoc.getElementsByTagName("pic")[0].getAttribute("path")); 'xmlDoc' is undefined But without this bit:var xmlDoc; function loadxml() { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.onreadystatechange = show; xmlDoc.load("abc.xml"); }function show(){if(xmlDoc.readyState==4)document.getElementById("image").src = xmlDoc.getElementsByTagName("pic")[0].attributes.getNamedItem("path").nodeValue;}window.onload = loadxml; Firefox 4+ works well.So I guess it is the only problem with IE. getAttribute may be the key.With this bit: var xmlDoc; function loadxml() { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.onreadystatechange = show; xmlDoc.load("abc.xml"); }function show(){if(xmlDoc.readyState==4)document.getElementById("image").src = xmlDoc.getElementsByTagName("pic")[0].attributes.getNamedItem("path").nodeValue;}window.onload = loadxml; IE works but not FF.And this code (not from me, I just adopted from some other sites) works in IE with its different syntax of getAttribute attributes.getNamedItem("path").nodeValue which does not mean anything to me (unusual to me)but it works in IE. So I'm wondering how to make getAttribute work across IE & FF. Link to comment Share on other sites More sharing options...
Ingolme Posted October 14, 2011 Share Posted October 14, 2011 The problem isn't the getAttribute, getAttribute works just fine. It's that Internet Explorer doesn't allow AJAX requests on the filesystem. Test this on localhost or on a webserver. Link to comment Share on other sites More sharing options...
tinfanide Posted October 14, 2011 Author Share Posted October 14, 2011 Does my script above contain any AJAX?I think the script is just about XML and JS. About the parsing. I've done one successfully in IE with XML parsing in JS:Please download the two files (HTML, XML) to have a look:http://www.box.net/files/0/f/103245920/Excel#/files/0/f/123117903/XML In this example, I only use XML tags without any XML attributes.That was why I'd thought the root was in getAttribute becausein this example it both works in IE and FF. But on my XAMPP, AJAX works well in IE9.Try the three files (HTML, JS & PHP) on your server if you like: http://www.box.net/files/0/f/103245920/Excel#/files/0/f/120483775/AJAX The AJAX requests work fine in my IE9. Link to comment Share on other sites More sharing options...
Ingolme Posted October 14, 2011 Share Posted October 14, 2011 XML is loaded using the same techniques as AJAX. That's why you have an xmlhttp object. One problem here is that you should declare xmlDoc before calling the function that sets it, not the other way around:This is wrong: parseXML("GET","abc.xml",false);var xmlDoc; This is right: var xmlDoc;parseXML("GET","abc.xml",false); Link to comment Share on other sites More sharing options...
tinfanide Posted October 15, 2011 Author Share Posted October 15, 2011 Well, Ingolme, I must thank for your patient in my case here.It's getting a bit more and more sophisticated from pure JS to XML parsing. I haven't got the AJAX thing and XML thing very alright, though.But I know what's happened to my case. function parseXML(method,xml,async){if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); xmlhttp.open(method,xml,async); xmlhttp.send(); }else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") ; xmlhttp.async=async; xmlhttp.load(xml); }xmlDoc = xmlhttp.responseXML;}parseXML("GET","abc.xml",false); function show(){document.getElementById("image").src = xmlDoc.getElementsByTagName("pic")[0].getAttribute("path");}window.onload = show; This is my original script before I posted my first post.That time I thought it might be because of "getAttribute" in IE, which is not, I realise now.And today I've put the original script on the XAMPP localhost (I neglected, a bit of overlooking, your advice in the previous post) and run it on DreamweaverI used to run the HTML file by clicking it in the explorer (even the file is placed in XAMPP)Then it works in both IE and FF.The address reminds me "http://localhost/test/testXMLparsing.html" "localhost" instead of "C:\xampp\htdocs\test\testXMLparsing.html" even though for "C:\xampp\htdocs\test\testXMLparsing.html"Firefox still reads it (IE not) Link to comment Share on other sites More sharing options...
Ingolme Posted October 15, 2011 Share Posted October 15, 2011 Be sure that you're declaring the xmlDoc variable in the global scope before running any functions. If you're calling a localhost URL, be sure that your HTML file is also on localhost. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.