Load xml in Javascript doesnt work in firefox

H

Hush

Hi,

The following code works fine in IE7 but FF returns with an error:

Access denied to achieve the property Element.firstChild. In this
line:
nodes = xmlDoc.documentElement.childNodes;

My code:

function CheckNick()
{
nick = document.getElementById("nick");
nickName = nick.value;

if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) handleXML()
};
xmlDoc.load("isNickAvailable.php?nick="+nickName+"&IEHACK="+(new
Date()).getTime());
}
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = handleXML;
xmlDoc.load("isNickAvailable.php?nick="+nickName+"&IEHACK="+(new
Date()).getTime());
}

else
{
alert('Your browser can\'t handle this script');
return;
}

}
function handleXML()
{
nodes = xmlDoc.documentElement.childNodes;
alert(nodes.item(0).text);
//
alert(xmlDoc.getElementsByTagName('nick').childNodes.firstChild.nodeValue);
}


Regards

Hush
 
M

Martin Honnen

Hush said:
The following code works fine in IE7 but FF returns with an error:

Access denied to achieve the property Element.firstChild. In this
line:
nodes = xmlDoc.documentElement.childNodes;

It is not likely that you get an error on 'firstChild' in an expression
that does not contain that name.

function handleXML()
{
nodes = xmlDoc.documentElement.childNodes;
alert(nodes.item(0).text);
//
alert(xmlDoc.getElementsByTagName('nick').childNodes.firstChild.nodeValue);

That expression does not make sense, getElementsByTagName gives a
collection which you can index, and childNodes gives a collection which
you can index too.
I think you should show us the XML you load and describe the elements
you want to read out, then we can help coding the access to those elements.
 
H

Hush

It is not likely that you get an error on 'firstChild' in an expression
that does not contain that name.
Sorry, I typed it wrong. Its of course Access denied to achieve the
property Element.childNodes, and not firstNode.
That expression does not make sense, getElementsByTagName gives a
collection which you can index, and childNodes gives a collection which
you can index too.
I think you should show us the XML you load and describe the elements
you want to read out, then we can help coding the access to those elements.

Ok. Actually I only want to extract 1 number (1 or 0) so I only need 1
value from the XML. My XML is as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<nick>
<isa>0</isa>
</nick>

I'm trying to get the value of the element <isa>

Hush
 
M

Martin Honnen

Hush said:
Ok. Actually I only want to extract 1 number (1 or 0) so I only need 1
value from the XML. My XML is as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<nick>
<isa>0</isa>
</nick>

I'm trying to get the value of the element <isa>

var isaElements = xmlDoc.getElementsByTagName('isa');
if (iseElements.length > 0)
{
var isa = isaElements[0];
var n = isa.firstChild.nodeValue;
}
else
{
// handle case that 'isa' element was not found
}

That assumes that the 'isa' element always has some text in it.
 
H

Hush

   var isaElements = xmlDoc.getElementsByTagName('isa');
   if (iseElements.length > 0)
   {
     var isa = isaElements[0];
     var n = isa.firstChild.nodeValue;
   }
   else
   {
     // handle case that 'isa' element was not found
   }
I'm stil getting the error for firstChild in this line:
var n = isa.firstChild.nodeValue;

I tested in IE. And it worked nicely...
 
M

Martin Honnen

Hush said:
I'm stil getting the error for firstChild in this line:
var n = isa.firstChild.nodeValue;

Can you post the exact error message? Your first post said "access
denied" which sounds more like a security exception. I am not sure why
you would get that, if you tried to load the XML from a different origin
then I would expect the load call to already throw an exception.
If you have a URL we can visit then post that too.

Does that problem occur with Firefox 3? Have you checked whether it also
occurs with Firefox 2?
 
H

Hush

Can you post the exact error message? Your first post said "access
denied" which sounds more like a security exception. I am not sure why
you would get that, if you tried to load the XML from a different origin
then I would expect the load call to already throw an exception.
If you have a URL we can visit then post that too.

Does that problem occur with Firefox 3? Have you checked whether it also
occurs with Firefox 2?

I translated the error from a danish version of firebug. I'm using
FF3, but when I come to think of it I had a similar error a while ago
with the same error. This was in an unchanged code, but after the
upgrade to FF3, so this could definately be the cause. I havent tested
in FF2. ( I will try that now)

URL:
http://www.sporturn.com/new/popuppages/new_profile.php
 
M

Martin Honnen

Hush said:
It works in FF2. So the problem is in FF3 :|

Oddly enough I have just tested with Mozilla/5.0 (Windows; U; Windows NT
5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 and entered some
characters into the "nick" field and I don't get any error in the error
console, instead each time an alert dialog with '1' is displayed.
Is there a fix??

I am still not sure why you get that error and I can't reproduce it.
As a workaround you might want to try to load the XML with
XMLHttpRequest instead of xmlDoc.load(), perhaps that avoids the problem.
 
T

Thomas 'PointedEars' Lahn

Hush said:
It works in FF2. So the problem is in FF3 :|

True, but that does not necessarily mean that the problem *is* Fx 3.
For example, Firebug for Fx 3 is still beta.
Is there a fix??

For a start, you could try not to mix standards compliant and proprietary
features, which would be a good idea in any case.


PointedEars
 
H

Hush

Oddly enough I have just tested with Mozilla/5.0 (Windows; U; Windows NT
5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 and entered some
characters into the "nick" field and I don't get any error in the error
console, instead each time an alert dialog with '1' is displayed.
An Alert should be displayed if it works.
The error message is in Firebug only, and its still a beta release.
But that still doesnt change the fact that it doesnt work in FF3...:(
I am still not sure why you get that error and I can't reproduce it.
As a workaround you might want to try to load the XML with
XMLHttpRequest instead of xmlDoc.load(), perhaps that avoids the problem.
I'm new in js, so can you give me an example?
 
H

Hush

True, but that does not necessarily mean that the problem *is* Fx 3.
For example, Firebug for Fx 3 is still beta.
Yes. You are right. But its still pretty annoying that there has to be
a difference:D
For a start, you could try not to mix standards compliant and proprietary
features, which would be a good idea in any case.

I'm sorry, I didnt understand a word of that..... (I'm new in JS)
Please bare with me....
 
M

Martin Honnen

Hush said:
An Alert should be displayed if it works.
The error message is in Firebug only, and its still a beta release.
But that still doesnt change the fact that it doesnt work in FF3...:(

Then disable Firebug and check whether it works then. I don't have
Firebug for FF 3 and it works for me.
 
H

Hush

Then disable Firebug and check whether it works then. I don't have
Firebug for FF 3 and it works for me.

Interesting... After I disabled it, the code goes to the 'else' in
this code:

if (isaElements.length > 0)
{

var isa = isaElements[0];
var n = isa.firstChild.nodeValue;

}
else
{
alert("isa element not found");// handle case that 'isa' element
was not found
}

Which has to mean that this line doesnt work (I guess):
var isaElements = xmlDoc.getElementsByTagName('isa');
 
K

kerry.geiger

I commented the line above out, and now it seems to work :| I dont
understand...........

http://www.sporturn.com/new/popuppages/new_profile.php

Does this make any sense to you?

I had the exact error - and it seems to be caused by Firebug. I
upgraded to FF3 yesterday, and my scripting worked, then installed
the
new version of firebug, and the scripts calling XML failed with the
permission error.

Unistall Firebug, and test.
 
H

Hush

 I had the exact error - and it seems to be caused by Firebug. I
 upgraded to FF3 yesterday, and my scripting worked, then installed
the
 new version of firebug, and the scripts calling XML failed with the
 permission error.

 Unistall Firebug, and test.

Yup. thats the solution. Everything works fine here now. (What an
annoying mistake !!)

Thank you all for the support:)
 
T

Thomas 'PointedEars' Lahn

Hush said:
Yup. thats the solution. Everything works fine here now. (What an
annoying mistake !!)

It would appear that you have been using a substandard Firebug version or
one that is not suited for Fx 3, because I have Firebug 1.2.0b7 installed
on top of Firefox 3.0.1 and it WFM as it did before.


PointedEars
 
H

Hush

It would appear that you have been using a substandard Firebug version or
one that is not suited for Fx 3, because I have Firebug 1.2.0b7 installed
on top of Firefox 3.0.1 and it WFM as it did before.

I user Firebug 1.2.0b7, and after deactivating, the problem
disappeared.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top