/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] using ‘childNodes’ and working with ‘/n’

Hi all,
I have a question regarding carriage(sp?) returns in IE vs FF. I installed Firebug for my FF which works awesome, but some of the scripts i used that utilized childNodes in the DOM were throwing errors. When I expanded the DOM in Firebug I saw that ‘/n’s were accounted for in counting the children. These seemed to work when used in IE.

What I’m wondering is, does IE ignore carriage returns or am i imagining things. Now that i fixed them all to work in FF they don’t work in IE. Is there a way to get IE to acknowledge them or get FF to ignore them? Or do i simply have to account for both ways and check for it in my scripts?

I am relatively new so this may be an obvious question but i can’t find an answer anywhere. Thanks in advance πŸ™‚

to post a comment
JavaScript

6 Comments(s) ↴

Copy linkTweet thisAlerts:
@KravvitzSep 19.2007 β€”Β Yeah, IE doesn't count white-space-only text nodes in the childNodes[] collection.

One solution is to use a function like this.
function getKids(el) {
if(!el || !el.childNodes) return;
for(var kids=[],i=0,len=el.childNodes.length;i<len;i++)
if(el.childNodes[i].nodeType == 1) kids.push(el.childNodes[i]);
return kids;
}
// versions of IE before 5.5 don't support Array.push() natively.
// You might want this for versions of IE before 5.5.
if(![].push){ // for old browsers that don't natively support Array.push()
Array.prototype.push=function(){
for(var i=0;i<arguments.length;i++) this[this.length]=arguments[i];
return this.length;
}
}
Copy linkTweet thisAlerts:
@KorSep 19.2007 β€”Β I would have not used push() method. The solely use of the self length should be enough and full cross-browser, I guess
<i>
</i>function getKids(el) {
if(!el || !el.childNodes[0]) return;
for(var kids=[],i=0,len=el.childNodes.length;i&lt;len;i++)
if(el.childNodes[i].nodeType == 1) [COLOR="Blue"]kids[kids.length]=el.childNodes[i][/COLOR];
return kids;
}

On the other hand a while loop works faster than an for loop:
<i>
</i>function getKids(el) {
if(!el || !el.childNodes[0]) return;
var kids=[],i=0,c;
while(c=el.childNodes[i++]){
c.nodeType==1?kids[kids.length]=c:null;
}
return kids;
}
Copy linkTweet thisAlerts:
@WebnerdSep 19.2007 β€”Β To answer the question, IE will incorrectly ignore white-space text nodes when traversing the DOM tree. So, you should always create your script to recognize the nodeType being accessed.
Copy linkTweet thisAlerts:
@KravvitzSep 19.2007 β€”Β True. I just prefer using Array.push().

While loops are slightly faster than for loops but I prefer the clarity of a for loop.

Why did you use a ternary operator there?
Copy linkTweet thisAlerts:
@djc11authorSep 19.2007 β€”Β Thank you for the help. I used the for loop for clarity but did not use the push() since compatibility is what got me here.

Sorry to bother again but when i use the function i am trying to fetch a length and am getting an error in IE.


-------------------
var arr = new Array;

arr = getKids(element);
----------------------



arr.length is undefined. Why is that? arr is declared as an array and is set to another array. I would think this would take care of it.
Copy linkTweet thisAlerts:
@KorSep 19.2007 β€”Β Thank you for the help. I used the for loop for clarity but did not use the push() since compatibility is what got me here.

Sorry to bother again but when i use the function i am trying to fetch a length and am getting an error in IE.


-------------------
var arr = new Array;

arr = getKids(element);
----------------------



arr.length is undefined. Why is that? arr is declared as an array and is set to another array. I would think this would take care of it.[/QUOTE]


var arr=new Array[COLOR="Red"]()[/COLOR];

or, shorter in JSON

var arr=[COLOR="Red"][][/COLOR];

Why did you use a ternary operator there?
[/quote]

For the shorter notation, I guess ?
Γ—

Success!

Help @djc11 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 5.18,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...