load XML file, works in IE but no in Firefox

S

SM

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);
...
}
 
S

SM

That line needs to be
xmlDoc.onload = init;

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
 
M

Martin Honnen

SM said:
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);
};
 
S

SM

xmldoc.onload = function (evt) {
init(value);
};

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
 
M

Martin Honnen

SM said:
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);
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top