Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-14-2009, 04:34 AM   PM User | #1
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
getting comment nodes

what's the fastest or easiest way to get all comment nodes into an Array?

the solution only needs to work in ie8/w3 if that helps.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-14-2009, 03:01 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
//
Code:
function getComments(hoo){
	if(!arguments.length) hoo=document;
	var A= [];
	if(hoo){
		hoo= hoo.firstChild;
		while(hoo!= null){
			if(hoo.nodeType== 8){
				A[A.length]= hoo.data;
			}
			else A= A.concat(arguments.callee(hoo));
			hoo= hoo.nextSibling;
		}
	}
	return A;
}

// Test
alert(getComments())


Note- IE (only) will return doctype text, if the page has a doctype, as the first item in the array. You can ignore it or remove it before the return -

Code:
if(A[0].toLowerCase().indexOf('doctype')==0)A.shift();
return A;
mrhoo is offline   Reply With Quote
Old 10-15-2009, 10:37 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
thanks for the code.
You ended up opening pandora's box on me here.

I generalized your recursive iteration pattern to be able to grab any node based on a property. This grew and grew until I had a very robust and powerful function.

I will probably end up posting the results in the "Post a javascript" section, i want to finish my unit-tests first.

At any rate, thanks again!
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:49 PM.


Advertisement
Log in to turn off these ads.