SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Dallas, TX
    0 Post(s)
    0 Thread(s)

    Empty Text Nodes in Gecko

    Here's another couple of functions that I've come up with that help me with my DOM scripting. These functions were born when I had problems with object heirarchy doing funny things to my scripts in Gecko browsers. The problem was as follows. Let's use the following HTML snippet for my example
    To IE's DOM, this HTML looks like this
    -> <SPAN>
       -> #textNode(Hello)
    -> <SPAN>
       -> #textNode(World!)
    But, to Gecko's DOM, it looks like this
    -> #textNode(\n)
    -> <SPAN>
       -> #textNode(Hello)
    -> #textNode(\n)
    -> <SPAN>
       -> #textNode(World!)
    -> #textNode(\n)
    In otherwords, Gecko's DOM inteprets lines breaks in the HTML as 'empty' textNodes, that acutally contain a newline character. Now, it should be apparent that in the Gecko DOM, there are more children to the DIV than in the IE DOM (3 in this example), and that's where things get messed up. The following two functions will remove all 'emtpy' textNodes so that your references (i.e this.firstChild, or this.previousSibling) will behave properly (<-- subjective use of 'properly' here ) in Gecko. (I'm gonna use the PHP tags here for the nifty syntax coloring)
    PHP Code:
    // Inits parsing to remove empty textnodes for Gecko
    function initTNparse(o) {
    window.TNcount 1;
        while (
    window.TNcount != 0) {
    window.TNcount 0;
    // Recursive funtions that removes the textnodes
    function TNparse(oNode) {
    j<oNode.childNodes.length) {
    currNode oNode.childNodes[j];
            if (
            if (
    currNode.nodeType == && !^\s+$/)) {

    body onLoad="initTNparse(document.body);"
    I know what you're thinking. Why the looping initializer function if the parsing function is recursive? Good question. I don't know either. For some reason I couldn't get TNparse() to properly recur in Gecko.

    If you don't want to remove all the empty textNodes from the entire document, then just pass the initTNparse() function a different object.

    Happy (DOM) scripting!
    Last edited by beetle; Sep 24, 2002 at 10:52.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev


Posting Permissions

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