Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

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 05-14-2004, 10:30 AM   PM User | #1
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Question DOM childNodes for Mozilla

A table (id="tableid") with 7 rows

It looks like

document.getElementById('tableid').childNodes[0].childNodes.length

return 0 for Mozilla instead of 7 in IE

Any ideea? Isn't it a standard DOM code line?

Do you need the whole code?... It is rather long... no error in Mozzila console, just that array length don't work...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-14-2004, 10:37 AM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
childNodes[0] probably contains a Text node in your case, containing the whitespace you almost certainly have between the table opening tag and it's first child element..
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 05-14-2004, 11:56 AM   PM User | #3
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
Also, a <table> has <tbody> before <tr> and mozilla will add that in the DOM even if it isn't in your source code.
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 05-14-2004, 05:15 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
thanks for replay... but what is i he use of tbody? (pardon my question, but I have never used that tag)?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-14-2004, 05:22 PM   PM User | #5
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
That's just the way a table should be structured:
Code:
<table>
	<tbody>
		<tr>
			<td> ... </td>
		</tr>
		<tr>
			<td> ... </td>
		</tr>
		<tr>
			<td> ... </td>
		</tr>
	</tbody>
</table>
There are also <thead> and <tfoot>, but they're both optional.

What mozilla does is correct the DOM for you. IE doesn't do that. So the way to avoid conflict is to structure it the right way yourself.

I found out this way too

Incidentally - what liorean said still applies as well - the node you want may not be childNodes[0] if there's a whitespace text node in between. Using getElementsByTagName is one to find elements, or you can clean the whitespace yourself using a function like this one - http://www.codingforums.com/showthread.php?t=7028
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 05-14-2004 at 05:25 PM..
brothercake is offline   Reply With Quote
Old 05-15-2004, 02:31 AM   PM User | #6
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Well, it's got with te table model to do. The thead, tfoot and tbody elements are table row groups, meaning (surprisingly enough) that they group table rows together. They are in HTML required, but with start and end tags optional. See http://www.codingforums.com/showthread.php?p=152579 for some more detailed commentary I wrote about it.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 05-15-2004, 09:14 AM   PM User | #7
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Yes Sirs, tbody was the clue... Now my code works on Mozilla also. Thank you all very much for your support. I read your comments, liorean, thank you.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-15-2004, 09:18 AM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
eeeerrr i came back... No it does not work. Mozilla says now on console

obj.childNodes[col] has no properties

Hm...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-15-2004, 10:40 AM   PM User | #9
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Well, then you must test the following to find out:

obj
typeof obj
obj.childNodes
typeof obj.childNodes
obj.childNodes.length
obj.childNodes[0]
typeof obj.childNodes[0]


Is anything of that other than what you might expect? Why might that be?
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 05-17-2004, 08:41 AM   PM User | #10
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
as I said, I set up the tr's array as

document.getElementById('tableid').childNodes[0].childNodes

IE accepts it as an array of TR's, Mozilla don't (when tested the lenght, Mozilla says I have 14 elements in array, while I have 7 rows)... That means the way objects are set up is wrong, from Moz's point of view, but I don't see which is that point...

here's the main function:
PHP Code:
function sortTable(col){
    if (
lastSort == col){
        
// sorting on same column twice = reverse sort order
        
absOrder absOrder false absOrder true;
    }
    else{
        
absOrder true;
    }
    
lastSort col;
    
allTR document.getElementById('tableid').childNodes[0].childNodes;
    
// allTR now holds all the rows in the dataTable
    
totalRows allTR.length;
    
colToSort = new Array()        //holds all the cells in the column to sort
    
colArr = new Array()                //holds all the rows that correspond to the sort cell
    
copyArr = new Array()            //holds an original copy of the sort data  to match to colArr
    
resultArr = new Array()            //holds the output

    
allNums true;
    
allDates true;
    
alert(document.getElementById('tableid').childNodes[0].childNodes.length]);
    
//store the original data
    //remember that the first row - [0] -  has column headings
    //so start with the second row - [1]
    //and load the contents of the cell into the array that will be sorted
    
for (x=1totalRowsx++){
        
colToSort[x-1] = setDataType(allTR[x].childNodes[col].innerHTML);
        
colArr[x-1] = allTR[x];
    }
    
//make a copy of the original
    
for (x=0x<colToSort.lengthx++){
        
copyArr[x] = colToSort[x]
    }

    
//sort the original data based on data type
    
if (allNums){
        
colToSort.sort(numberOrder)
    }
    else if (
allDates){
        
colToSort.sort(dateOrder)
    }
    else{
        
colToSort.sort(textOrder)
    }

    
//match copy to sorted
    
for(x=0x<colToSort.lengthx++){
        for(
y=0y<copyArr.lengthy++){
            if (
colToSort[x] == copyArr[y]){
                
boolListed false;
                
//searcg the ouput array to make sure not to use duplicate rows
                
for(z=0z<resultArr.lengthz++){
                    if (
resultArr[z]==y){
                        
boolListed true;
                        break;
                    }
                }
                if (!
boolListed){
                    
resultArr[x] = y;
                    break;
                }
            }
        }
    }
    
//now display the results - it is as simple as swapping rows
    
for (x=0x<resultArr.lengthx++){
        
allTR[x+1].swapNode(colArr[resultArr[x]])
    }

__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-17-2004, 01:00 PM   PM User | #11
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
Anything wrong with using DOM2 HTML?
http://www.w3.org/TR/2003/REC-DOM-Le...ml#ID-64060425

Even IE supports the relevant parts of the Table model I think. And it eliminates the potential tbody/whitespace issues you might run across.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 06-04-2004, 09:15 AM   PM User | #12
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Why not use

allTR = document.getElementById('tableid').getElementstByTagName('tr');

to eliminate the tbody/whitespace issues?
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 06-04-2004, 10:32 AM   PM User | #13
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
...or even document.getElementById('tableid').rows Yes, this was the solution...

Yet I consider it a bug for Moz. If tbody can be an optional tag I don't think it must be or not taken in consideration as a compulsory Node. And even so, I still don't understand where from Mozilla invented double number of childNodes...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 06-04-2004, 10:48 AM   PM User | #14
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Mozilla isn't wrong on this. The DOM explicitly states that the browser should inject the TBODY element there. The thing is that in HTML4.01, the tbody element is required, but you may leave out both it's start tag and ints end tag. Meaning that when you write
Code:
<table>
    <tr></tr>
    <tr></tr>
</table>
you have actually written the structure
Code:
<table>
    <tbody>
        <tr></tr>
        <tr></tr>
    </tbody>
</table>
because the tbody is required but doesn't need to be explicitly written out in the source code. So, what you're complaining about is actually HTML4.01, not the DOM. The DOM is just made to handle HTML and XML.

As for whitespace, Mozilla is arguably not wrong there either. You could argue that elements that may not contain #PCDATA should not contain Text nodes, but on the other hand, in XML, all consecutive whitespace should be concatenated to a single space, unless it happens to be significant. If you use CSS to change the presentation, you may make that space significant, which means that it hsould be there for display. Thus, all whitespace nodes should be in the element. If you need only elements, you have to filter the whitespace nodes out one way or another.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 03-18-2005 at 05:19 PM..
liorean is offline   Reply With Quote
Old 03-18-2005, 05:09 PM   PM User | #15
lorax1284
New to the CF scene

 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
lorax1284 is an unknown quantity at this point
Mozilla ISN'T wrong. IE is.

Quote:
Originally Posted by liorean
Mozilla isn't wrong on this.
I agree... don't forget, XML parsers do not require a DTD nor Stylesheet to parse the XML. Neither XML nor DOM standards state that <TABLE> means "this thing is a table"; that is done at the stylesheet level.

Essentially, Web browsers have "built in style sheets" particular to HTML. You toss an HTML-like page at the browser, and it will do its best to render it. But in terms of DOM compliance, web browsers should comply with the DOM standard. No DOM processing engines, including those embedded in Web browsers, should apply unilateral, arbitrary, exceptions to DOM rules, like: "according to what I (the browser) know about HTML, there can't be text between something called "TR", so my DOM tree generation code will not consider certain characters I deem to be 'whitespace' characters between "TR" nodes as text nodes". Slippery slope!

Myself, after discovering this irritating difference between Mozilla and IE, decided that the script-generated HTML code on which my DOM scripts will act will have all the tags mashed together, inserting line feeds between selected attributes, inside the tags themselves, not between elements, so therefore no "whitespace text nodes" are created.

This solves the "extra nodes" problem, but in terms of markup legibility, darn it, does anyone know of a text editor that will "pretty print" XML on screen, according to nesting etc, without actually inserting whitespace characters? Just for debugging the generated markup....
lorax1284 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 08:21 AM.


Advertisement
Log in to turn off these ads.