...

View Full Version : error:firstChild has no properties in Mozilla.



Jayakeerthi
02-13-2006, 05:37 PM
Hi everybody,
me facing issue in cross browser like the below code gives firstChild has no properties in Mozilla.
and no object found in IE browser...

i have tried..... sampleXML.item(k).firstChild.nodeValue;
sampleXML.item(k).firstChild.firstChild.nodeValue;
sampleXML.item(k).firstChild.nodeValue;
sampleXML.item(k).childNodes[0].childNodes[0].nodeValue;
sampleXML.item(k).childNodes[1].childNodes[0].nodeValue;
but nothing worked for me......
could anyOne helpme out in fixing this issue....




function selectProjectsForSurvey(){


// IE5.2 on Mac OSX doesn't support ActiveXObject
// So Dom Parser is used to parse, on Mozilla browser

if (document.implementation.createDocument)
{

// Mozilla, create a new DOMParser
parser = new DOMParser();
var xmlDoc = parser.parseFromString(document.forms[0].projectSurveyMappings.value, "text/xml");

}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(document.forms[0].projectSurveyMappings.value);
}

var selectedProjectId =

document.forms[0].projectId.options[document.forms[0].projectId.selectedIndex].value;
if(selectedProjectId < 0){
alert("Inside added if...");
return " ";
}
var allowNext = false;
var projectXML = xmlDoc.getElementsByTagName("PROJECT");
var surveyXML;
var sampleXML;


for (i=0; i < projectXML.length; i++)
{
if (projectXML.item(i).getAttribute('id') == selectedProjectId)
{
surveyXML = projectXML.item(i).getElementsByTagName("SURVEY");

// Remove all the surveys from the list(associated with previous project)
document.forms[0].dataCollectionId.length = 0;

for (j=0; j < surveyXML.length; j++)
{
allowNext = true;
var opt = new Option();
opt.value = surveyXML.item(j).getAttribute("id");

//Line Modified to fix the Mac OSX issue
opt.text = sampleXML.item(k).firstChild.childNodes[1].childNodes[0].nodeValue;

//the above line has error: firstChild has no properties

document.forms[0].dataCollectionId[j] = opt;


}
sampleXML = projectXML.item(i).getElementsByTagName("SAMPLE");
document.forms[0].sampleId[0].length = 0;

for (k=0; k < sampleXML.length; k++)
{
var opt = new Option();
opt.value = sampleXML.item(k).getAttribute("id");

//Line Modified to fix the Mac OSX issue
//objXML.text doexn't support on mozilla.
opt.text = sampleXML.item(k).firstChild.childNodes[1].childNodes[0].nodeValue; document.forms[0].sampleId[0][k] = opt;
}
}
}


if (allowNext == true)
document.forms[0].next.disabled = false;
else
document.forms[0].next.disabled = true;

//document.forms[0].submit();
}

function onloadForSurvey()
{

selectProjectsForSurvey();
if (document.forms[0].dataCollectionId)
{
if (document.forms[0].dataCollectionId.length == 0)
document.forms[0].next.disabled = true;
else
document.forms[0].next.disabled = false;
}
}


thanks in advance
Keerthi

liorean
02-13-2006, 05:52 PM
It sounds like you've encountered the problem of whitespace text nodes. You see, if you have the following XML:
<root>
<foo>
<bar>
</bar>
</foo>
</root>Then the document tree will look like this:
#element: "root"
> #text: "\n "
> #element: "foo"
> > #text: "\n "
> > #element: "bar"
> > > #text: "\n "
> > #text: "\n "
> #text: "\n"So, you need to check if the node is a text node, and if so go to the next node.

Iew doesn't keep text nodes in the DOM as it should.

Jayakeerthi
02-13-2006, 06:56 PM
Hi liorean,
thanks for your kind reply.............

you are right the node is the text node where i am trying to get the text of the tag

previously i had used sampleXML.item(k).text
to get the name of it where it was not supported by the Mac-IE so i changed the code as posted earlier,where the application is working ie the names in the drop down list is changing dynamically....(in 3 drop down according to XML) but i am getting the javascript error saying has no properties

thanks
Keerthi

Jayakeerthi
02-13-2006, 07:06 PM
could you please tell me where exactly this below code to be placed i guess the code is correct to get rid off whitespace....

const notWhitespace = /\S/

function cleanWhitespace(node) {
// alert("white Space");
for (var x = 0; x < node.childNodes.length; x++) {
var childNode = node.childNodes[x]
if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
// that is, if it's a whitespace text node
node.removeChild(node.childNodes[x])
x--
}
if (childNode.nodeType == 1) {
// elements can have text child nodes of their own
cleanWhitespace(childNode)
}
}
}

document.addEventListener("load", function() {
cleanWhitespace(xmlDoc)
}, true)


thanks
Keerthi

Jayakeerthi
02-14-2006, 05:12 AM
Hi everybody,
me facing issue in cross browser like the below code gives firstChild has no properties in Mozilla.
and no object found in IE browser...

i have tried..... sampleXML.item(k).firstChild.nodeValue;
sampleXML.item(k).firstChild.firstChild.nodeValue;
sampleXML.item(k).firstChild.nodeValue;
sampleXML.item(k).childNodes[0].childNodes[0].nodeValue;
sampleXML.item(k).childNodes[1].childNodes[0].nodeValue;
but nothing worked for me......
could anyOne helpme out in fixing this issue....

function selectProjectsForSurvey(){


// IE5.2 on Mac OSX doesn't support ActiveXObject
// So Dom Parser is used to parse, on Mozilla browser

if (document.implementation.createDocument)
{

// Mozilla, create a new DOMParser
parser = new DOMParser();
var xmlDoc = parser.parseFromString(document.forms[0].projectSurveyMappings.value, "text/xml");

}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(document.forms[0].projectSurveyMappings.value);
}

var selectedProjectId =

document.forms[0].projectId.options[document.forms[0].projectId.selectedIndex].value;
if(selectedProjectId < 0){
alert("Inside added if...");
return " ";
}
var allowNext = false;
var projectXML = xmlDoc.getElementsByTagName("PROJECT");
var surveyXML;
var sampleXML;


for (i=0; i < projectXML.length; i++)
{
if (projectXML.item(i).getAttribute('id') == selectedProjectId)
{
surveyXML = projectXML.item(i).getElementsByTagName("SURVEY");

// Remove all the surveys from the list(associated with previous project)
document.forms[0].dataCollectionId.length = 0;

for (j=0; j < surveyXML.length; j++)
{
allowNext = true;
var opt = new Option();
opt.value = surveyXML.item(j).getAttribute("id");

//Line Modified to fix the Mac OSX issue
opt.text = sampleXML.item(k).firstChild.childNodes[1].childNodes[0].nodeValue;

//the above line has error: firstChild has no properties

document.forms[0].dataCollectionId[j] = opt;


}
sampleXML = projectXML.item(i).getElementsByTagName("SAMPLE");
document.forms[0].sampleId[0].length = 0;

for (k=0; k < sampleXML.length; k++)
{
var opt = new Option();
opt.value = sampleXML.item(k).getAttribute("id");

//Line Modified to fix the Mac OSX issue
//objXML.text doexn't support on mozilla.
opt.text = sampleXML.item(k).firstChild.childNodes[1].childNodes[0].nodeValue; document.forms[0].sampleId[0][k] = opt;
}
}
}


if (allowNext == true)
document.forms[0].next.disabled = false;
else
document.forms[0].next.disabled = true;

//document.forms[0].submit();
}

function onloadForSurvey()
{

selectProjectsForSurvey();
if (document.forms[0].dataCollectionId)
{
if (document.forms[0].dataCollectionId.length == 0)
document.forms[0].next.disabled = true;
else
document.forms[0].next.disabled = false;
}
}

const notWhitespace = /\S/

function cleanWhitespace(node) {
// alert("white Space");
for (var x = 0; x < node.childNodes.length; x++) {
var childNode = node.childNodes[x]
if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
// that is, if it's a whitespace text node
node.removeChild(node.childNodes[x])
x--
}
if (childNode.nodeType == 1) {
// elements can have text child nodes of their own
cleanWhitespace(childNode)
}
}
}

document.addEventListener("load", function() {
cleanWhitespace(xmlDoc)
}, true)


i have added function as above to clear the white space could anyoue help me out in fixing this issue...

thanks in advance
keerthi

liorean
02-14-2006, 05:22 AM
Please don't crosspost. One post is usually enough. Also remember that some kind of issues won't get answers directly, because nobody with both knowledge enough and time enough to spare for it is at it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum