Wednesday, August 5, 2009

JavaScript DOM Child Node navigator to fix FireFox childNodes problem.

While working on DOM tree navigation using JavaScript, I realized that FireFox browser has a nasty problem of treating empty white spaces as text nodes. If your page size is very big, it makes physically impossible for you to track down a particular type of DOM Node using the childNodes function in JavaScript.

DOM Level 1 provides functions to navigate through the DOM Node tree such as, parentNode, childNodes, firstChild, lastChild etc. All these functions works fine in all the the browsers but sometime childNodes function gives inconsistent results in FireFox because it treats empty spaces as Text Nodes. For that reason I have created an easy to use Child Nodes navigation function which allows you to choose the type of nodes you want to select directly without navigating through rest of the Child Nodes.

This function getChildNode() comes in handy for navigating to a particular type of child node under an element. It takes three arguments, first is an object to the HTML DOM Element, second is the DOM Node Type under that Element and the third one is the index of the Node type in the child nodes list. You can call the getChildNode() function from your JavaScript code to access the DOM Nodes. For example if you want only the DOM Element Node, you can use this function to get the first (index starts at 0) DOM Element Node as follows:

var myNode = getChildNode(element, Nodes.ELEMENT_NODE, 0)

The getChildNodeCount() function can be used to count any particular type of DOM Nodes under any element and can be used as shown in example below to navigate through all the DOM Element Nodes under the HTML Body element:

var myBody = document.body;
for (i = 0; i < getChildNodeCount(myBody, Nodes.ELEMENT_NODE); i++) {
    var ElementNode = getChildNode(myBody, Nodes.ELEMENT_NODE, i)
    alert(ElementNode.innerText);
}

JavaScript Code:
<script type="text/javascript">
function getChildNode(node, childType, index) {
    var childnode;
    var childnode_index = 0;
    var children = node.childNodes;
    for (var i = 0; i < children.length; i++) {
        if (children[i].nodeType == childType) {
            childnode = children[i];
            if (childnode_index == index) break;
            else childnode_index++;
        }
    }
    return childnode;
}
function getChildNodeCount(node, childType) {
    var count = 0;
    var children = node.childNodes;
    for (var i = 0; i < children.length; i++) {
        if (children[i].nodeType == childType)
            count++;
    }
    return count;
}
var Nodes =
{
      ELEMENT_NODE                :  1,
      ATTRIBUTE_NODE              :  2,
      TEXT_NODE                   :  3,
      CDATA_SECTION_NODE          :  4,
      ENTITY_REFERENCE_NODE       :  5,
      ENTITY_NODE                 :  6,
      PROCESSING_INSTRUCTION_NODE :  7,
      COMMENT_NODE                :  8,
      DOCUMENT_NODE               :  9,
      DOCUMENT_TYPE_NODE          : 10,
      DOCUMENT_FRAGMENT_NODE      : 11,
      NOTATION_NODE               : 12
};
</script>

Technorati Tags:

4 comments:

  1. mytectra placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance.

    ReplyDelete
  2. Guaranteed #1 Search Engine Ranking Supreme Free Viral Traffic Join Now Get Millions Of Hits Free To Your Site/Blog!

    PornKings Adult Shopping Backlinks-Shopping Mega Store Legendary Stars As Stormy Daniels,Shawna Edwards,Jenna Jamison-New Adult Stars Movies,Adult Toys,Enhancers,Merchandise-More !

    Hits Express Rotator System Do You Need Visitors to Your Website or Affiliate Program? If your looking to gain more visitors to your website Hits Express is your answer. With our program your site is being shown to people all over the world 24/7 365 days a year!

    PAYDIR Free For All Forum Portal Search Engine Crawled Network PR10 Ranking Information Forum XXX!

    FreeLinkExchanges Be Seen In 12 Nations 312 Sites Over 30 Millions Viewers Monthly Buy Featured Link Now With 150 Search Engines Crawling The Network!

    How do I get guaranteed traffic? When someone signs up from your site, they must first click on your classified ad which will open a new window leading to your main website. They will have to wait a few seconds for the code to appear on a separate frame at the top of the screen reach millions free now!

    Blast Your Ad to Over 23,000 Opt-in Prospects at ShowMyLinks Submit Your Solo Email Ad to All Showmylinks Members GET YOUR TEXT LINK ADs LISTED 100% FREE FOR LIFE PLUS EARN MONEY TO YOUR PAYPAL!!

    Adult Store Empires Backlinks Resources Search Engine XXX!

    GET YOUR OWN MONEY-MAKING AD BOARD -- Integrating Text ad, Banner Ad and Email Ad into one Portal Make Money Fast With Your Paypal Reach Million Dollars In A Year Fast !

    Full Length Homemade Videos Watch real people's private home sex videos. Forget about those shitty 1-2 minute clips, these are all high quality full length videos! 40+ Bonus Sites You will also get full access to the entire reelpass network of sites which features over 40+ awesome movie sites, 1000's of movies and pictures to download! Download, Burn & Share Not only do you get to watch all our movies, you can also download every single one and save it forever, burn them to dvd or share with friends!!

    ReplyDelete

Thanks a lot for your valuable comments :)