|
|
 |
 |
 |
 |
Javascript / Client Side Development
|
 |
 |
 |
 |
 |
 |
 |
 |
load XML file, works in IE but no in Firefox
Hello, I wrote a Javascript function that loads a xml file to access the data. After the loading of the xml file, the function init() is called (for testing purposes im only returning the number of elements) The loading executes ok for IE and the init() function returns the correct numbers of elements. When i try to load the xml file in Firefox, the init() function executes but the number of elements is always 0. I guess is because Firefox can't read the xml correctly. But why? I've try rewriting the code in all the ways possible with no success. What's wrong with this simple? Need help Marco function loadXML() { //For firefox and friends if (document.implementation && document.implementation.createDocument) { alert('my name is Firefox'); xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.onload = init(); }
else if (window.ActiveXObject) { alert('my name is IE'); xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) { try { init(); } catch(e) {alert('Error loading the XML file: ' + e.toString()); } }}; } //end if
else { alert('i have no name :('); alert('Your browser can\'t load the XML file: ' + e.toString()); return; } //end else if
xmlDoc.load("DB/discography_original.xml"); }
function init() { ... var cdElems = xmlDoc.getElementsByTagName('cd'); alert('# of CDs: ' + cdElems.length); ...
}
> When i try to load the xml file in Firefox, the init() function > executes but the number of elements is always 0. I guess is because > Firefox can't read the xml correctly. But why?
I don't know if it's related but with FF it's important that the web server gives the right mime type for XML/XSL file ( text/xml ). IE doesn't care but for FF it's important. See http://www.rgagnon.com/javadetails/java-0450.html Bye. -- Real Gagnon from Quebec, Canada * Java, Javascript, VBScript and PowerBuilder code snippets * http://www.rgagnon.com/howto.html * http://www.rgagnon.com/bigindex.html
I have a book that covers JavaScript and a little bit of XML. It says that accessing XML with JavaScript _only_ works in Internet Explorer 5.0+ for Windows. One generally uses PHP to access XML, because PHP is implemented on the server and therefor consistent. Book's Website: http://www.webbedenvironments.com/dhtml%5Fcss%5Fadvanced/
SM wrote: > xmlDoc = document.implementation.createDocument("", "", null); > xmlDoc.onload = init();
That line needs to be xmlDoc.onload = init; -- Martin Honnen http://JavaScript.FAQTs.com/
On May 22, 7:41 am, Martin Honnen <mahotr@yahoo.de> wrote: > SM wrote: > > xmlDoc = document.implementation.createDocument("", "", null); > > xmlDoc.onload = init(); > That line needs to be > xmlDoc.onload = init; > -- > Martin Honnen > http://JavaScript.FAQTs.com/
that last one did it for me. xmldoc.onload = init; WORKS fine but xmldoc.onload = init(parameters) doesn't work! how do i pass a value in the init function? thanks marco
SM wrote: > xmldoc.onload = init; WORKS fine > but > xmldoc.onload = init(parameters) doesn't work! > how do i pass a value in the init function?
xmldoc.onload = function (evt) { init(value); }; -- Martin Honnen http://JavaScript.FAQTs.com/
Zabac wrote: > I have a book that covers JavaScript and a little bit of XML. It says > that accessing XML with JavaScript _only_ works in Internet Explorer > 5.0+ for Windows. > One generally uses PHP to access XML, because PHP is implemented on > the server and therefor consistent. > Book's Website: http://www.webbedenvironments.com/dhtml%5Fcss%5Fadvanced/
Then the book would be wrong, which should tell you a great deal about the rest of the book. For instance, this question was answered and fixed in a post after yours.
On May 22, 8:40 am, Martin Honnen <mahotr@yahoo.de> wrote: > SM wrote: > > xmldoc.onload = init; WORKS fine > > but > > xmldoc.onload = init(parameters) doesn't work! > > how do i pass a value in the init function? > xmldoc.onload = function (evt) { > init(value); > }; > -- > Martin Honnen > http://JavaScript.FAQTs.com/
Works like a charm! Thanks Martin Although, im not sure what the evt means in function(evt). If its not to much to ask, could you explain. I wrote the function with and without the 'evt', and they both work ok....just wondering ie xmldoc.onload = function(evt) { init(value); }; xmldoc.onload = function() { init(value); }; Thanks Marco
SM wrote: > Although, im not sure what the evt means in function(evt). If its not > to much to ask, could you explain.
Event handlers like onload have an event object argument that allows you to check properties of the event. If you don't need that argument then you can code xmldoc.onload = function () { init(value); }; -- Martin Honnen http://JavaScript.FAQTs.com/
|
 |
 |
 |
 |
|