Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts

    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 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    //
    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;

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    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 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •