javascript or ajax problem with firefox

M

matatunos

Hi!
I have a problem with a function that works fine in IE 7.0 and Opera
9.23, over Windows XP Professional SP2, but it doesn't work on Mozilla
firefox (i try with 2.0.0.7 spanish version) and it must work at least
with IE and Firefox

y have two combo-boxes, depending on the selection on the first one, it
calls a VB6 dll (using ASP) and returns one XML, then put the data into
the second combo-box (<select> <option>)

with IE and Opera it works fine, with Firefox it returns "Unknown"

i have tried an alert(xml.xml) returning responseXML in my ajax
function, and it returns 'undefined', but the 'for' works 4 times :-?

if i use responseText and show alert(xml), it works fine and returns
the xml correctly

if i replace
opcion.text=registros.item(i).childNodes[1].text;
with
opcion.text=registros.item(i).childNodes[1].nodeValue;

it returns 'null'
if i replace with
opcion.text=registros.item(i).childNodes[1].nodeName;
it returns the correct name of the node (nombre)

err = http_request.responseXML.parseError; returns 'undefined'
alert(http_request) returns 'Object'

so i cant use this

alert("Error on line " + err.line + "\n" + err.srcText + "\n" +
err.reason + "\n" + err.errorCode);



below is some code, ajax, javascritp function and xml

any issue?

thankyou!

***************************************************************
ajax.js *******************************************************

function makeHttp_request(url,method,parameters,handler)
{
//method: 'GET' o 'POST'
//parameters: null para GET, y 'var1=val1&...&varn=valn' para POST
//handler: nombre de la función que se encargará de tratar el
resultado

var http_request = false;
//crear el objeto http_request
if(window.XMLHttpRequest)
http_request = new XMLHttpRequest();
else if (window.ActiveXObject)
http_request = new ActiveXObject(Microsoft.XMLHTTP);


if (!http_request)
{
alert('ERROR :( No es posible crear una instancia
XMLHTTP');
return false;
}
http_request.onreadystatechange = function()
{
if (http_request.readyState==4)
{
if(http_request.status==200)
{
// err = http_request.responseXML.parseError;
// if (err.errorCode != 0)
// alert("Error on line " + err.line + "\n" + err.srcText + "\n"
+ err.reason + "\n" + err.errorCode);
//handler(http_request.responseText);

handler(http_request.responseXML); //código a ejecutar si el
código es correcto
}
else
{
if(http_request.status==404)
{
alert("¡¡¡ERROR!!!. La direccion no existe"); //(2) mostrar
error
}
else
{
alert("¡¡¡ERROR!!!. "+http_request.status); //(3) mostrar error
}
}
}
}

http_request.open(method.toUpperCase(), url, true);

if (method=="POST")
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

http_request.send(parameters);
return;

}
***************************************************************
myfunction.js*************************************************

function fincargarcomarca(xml)
{
'purge select
var select=vaciarselect("comarca");

'get elements from xmlDOMDocument
var registros = xml.getElementsByTagName("registro");

'create default option
var opcion=document.createElement('option');
opcion.value="0";
opcion.text="-[Ninguna Comarca Seleccionada]-";

'add to select
try
{
select.add(opcion, null); // standards compliant; doesn't work in
IE
}
catch(ex)
{
select.add(opcion); // IE only
}

'
for(var i=0;i<registros.length;i++)
{
var opcion=document.createElement('option');
opcion.value=registros.item(i).childNodes[0].text;
opcion.text=registros.item(i).childNodes[1].text;
try
{
select.add(opcion, null); // standards compliant; doesn't work in
IE
}
catch(ex)
{
select.add(opcion); // IE only
}
}
}

***************************************************************
mydata.xml*****************************************************

<?xml version="1.0" encoding="ISO-8859-1"?>
<datos>
<provincias>
<registro>
<codprov>15</codprov>
<nombre>A CORUÑA</nombre>
</registro>
<registro>
<codprov>27</codprov>
<nombre>LUGO</nombre>
</registro>
<registro>
<codprov>32</codprov>
<nombre>OURENSE</nombre>
</registro>
<registro>
<codprov>36</codprov>
<nombre>PONTEVEDRA</nombre>
</registro>
</provincias>
</datos>
 
M

Martin Honnen

matatunos said:
for(var i=0;i<registros.length;i++)
{
var opcion=document.createElement('option');
opcion.value=registros.item(i).childNodes[0].text;

The property named 'text' is part of the MSXML DOM but not of the W3C
Core DOM. W3C DOM Level 3 Core has a similar property named
'textContent' which is supported in Mozilla and Opera 9 but not IE.
With your XML document structure I would simply access the firstChild of
an element node and takes its nodeValue.

Also childNodes[0] might be a text node in Mozilla and an element node
with MSXML/IE so cross-browser it is safer to use getElementsByTagName e.g.

registros.item(i).getElementsByTagName('codprov').item(0).firstChild.nodeValue

gives you the node value of the first child of the first codprov child
element of the ith registro element. That assumes the XML structure you
receive is fixed and always has at least one codprov element which has
pure non empty text content.

Note that within JavaScript the DOM access item(i) or item(0) can be
reduced to

registros.getElementsByTagName('codprov')[0].firstChild.nodeValue
 
M

matatunos

this code worked fine! :)

i have different dinamically generated xml files, one of them may have
different nodeNames each time i is executed, i tried this code

for(var i=0;i<registros.length;i++)
{
var opcion=document.createElement('option');
var elemento=registros.childNodes[0].nodeName;
opcion.value=registros.getElementsByTagName(elemento)[0].firstChild.nodeValue;
opcion.text=registros.getElementsByTagName(elemento)[0].firstChild.nodeValue;
try
{
select.add(opcion, null); // standards compliant; doesn't work in
IE
}
catch(ex)
{
select.add(opcion); // IE only
}
}

and it also works fine in both IE and Firefox

one million thanks!

i'm a beguinner with xml/xsl/ajax/javascript/css/etc and i'm learning a
lot with this group :)
Martin Honnen a pensé très fort :
matatunos said:
for(var i=0;i<registros.length;i++)
{
var opcion=document.createElement('option');
opcion.value=registros.item(i).childNodes[0].text;

The property named 'text' is part of the MSXML DOM but not of the W3C Core
DOM. W3C DOM Level 3 Core has a similar property named 'textContent' which is
supported in Mozilla and Opera 9 but not IE.
With your XML document structure I would simply access the firstChild of an
element node and takes its nodeValue.

Also childNodes[0] might be a text node in Mozilla and an element node with
MSXML/IE so cross-browser it is safer to use getElementsByTagName e.g.

registros.item(i).getElementsByTagName('codprov').item(0).firstChild.nodeValue

gives you the node value of the first child of the first codprov child
element of the ith registro element. That assumes the XML structure you
receive is fixed and always has at least one codprov element which has pure
non empty text content.

Note that within JavaScript the DOM access item(i) or item(0) can be reduced
to

registros.getElementsByTagName('codprov')[0].firstChild.nodeValue
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top