473,561 Members | 3,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Netscape and IE show different number of child nodes for table?


Can anyone explain this to me? It's driving me insane.

Save this and run it in IE or Opera and then in Mozilla or Netscape 6+.
In IE/Opera, I get the expected 4 alerts.
In Mozilla/Netscape, I get *9*.
In the example table, there are 4 rows with 4 columns each in the tbody.
I'd expect 4 child nodes for the table body with 4 children each. I get
those 4 plus 5 [object Text] alerts in Mozilla/Netscape. I get only
those 4 in IE and Opera. Note that trying to alert the innerHTML shows
null for the Text objects.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function checkIt()
{
if (! document.getEle mentById)
{
alert("Sorry, your browser doesn't support this.");
return;
}

var T = document.getEle mentById("tb");
for (var i=0; i<T.childNodes. length; i++)
{
alert(T.childNo des[i]);
}
return;
}
</script>

</head>

<body>
<table id="t1" border="1" cellpadding="5" cellspacing="0" >
<tbody id="tb">
<tr>
<td><p>R1 C1</p></td>
<td><p>R1 C2</p></td>
<td><p>R1 C3</p></td>
<td><p>R1 C4</p></td>
</tr>
<tr>
<td><p>R2 C1</p></td>
<td><p>R2 C2</p></td>
<td><p>R2 C3</p></td>
<td><p>R2 C4</p></td>
</tr>
<tr>
<td><p>R3 C1</p></td>
<td><p>R3 C2</p></td>
<td><p>R3 C3</p></td>
<td><p>R3 C4</p></td>
</tr>
<tr>
<td><p>R4 C1</p></td>
<td><p>R4 C2</p></td>
<td><p>R4 C3</p></td>
<td><p>R4 C4</p></td>
</tr>
</tbody>
</table>
<form id="f1" name="f1">
<input type="button" name="b1" value="Try It" onClick="checkI t()">
</form>

</body>
</html>

--
--
~kaeli~
A lot of money is tainted - It taint yours and it taint mine.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #1
13 4142


kaeli wrote:
Can anyone explain this to me? It's driving me insane.
Let's try to save you from insanity then. Your test is already showing
you the difference the browsers have.

In IE/Opera, I get the expected 4 alerts.
In Mozilla/Netscape, I get *9*.
In the example table, there are 4 rows with 4 columns each in the tbody.
I'd expect 4 child nodes for the table body with 4 children each. I get
those 4 plus 5 [object Text] alerts in Mozilla/Netscape. I get only
those 4 in IE and Opera. Note that trying to alert the innerHTML shows
null for the Text objects.
If you have your markup as below then <tbody id="tb"> here the first child of the <tbody> element you have a text node with
white space with a line break which Opera and Mozilla/Netscape model in
the DOM <tr>
<td><p>R1 C1</p></td>
<td><p>R1 C2</p></td>
<td><p>R1 C3</p></td>
<td><p>R1 C4</p></td>
</tr>


Here is the next child text node with white space, a line break.

IE ignores those white space text nodes which most authors insert to
have their markup more readable e.g. you have written
<tbody>
<tr>
and not
<tbody><tr>
If you wrote all markup without white space between tags Mozilla, Opera
and IE would give you the same result for the child nodes.

The example doesn't show what you want to do, if you are looking for an
element consider using getElementsByTa gName and loop through the result
instead of looping through childNodes as with getElementsByTa gName you
get the same result in the browser independent of the white space text
node handling.
If you are really interested in the child nodes then you need to test
nodeType to decide what to do with a node, you get 1 for element nodes
and 3 for text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
In article <40********@ola f.komtel.net>, ma*******@yahoo .de enlightened
us with...
If you wrote all markup without white space between tags Mozilla, Opera
and IE would give you the same result for the child nodes.

Oh, holy mother of God.
Whose bright idea was that?!?!

Now I either have to exchange readable code with the
getElementsByTa gName, which I didn't want to use (too clunky), or make
unreadable code.
*sighs*

I guess it's getElementsByTa gName. I'm addicted to white space.

Thanks!
I wouldn't have ever thought of that. Not in a million years.
The example doesn't show what you want to do,


I know. The full code will replace a (specified) table cell, which would
have been much prettier just using the childNodes. The full code isn't
as easy to see the problem with as this, though it not huge or anything.

*grumbles*
That's gonna change one nice line of code into multiple lines to find
the right cell to replace.
*sigh*
Oh, well.

--
--
~kaeli~
Every calendar's days are numbered.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #3
kaeli <ti******@NOSPA M.comcast.net> writes:

[IE omits all-whitespace text nodes]
Oh, holy mother of God.
Whose bright idea was that?!?!
Microsoft. As usual.
Now I either have to exchange readable code with the
getElementsByTa gName, which I didn't want to use (too clunky), or make
unreadable code.
*sighs*
Or use:

var T = document.getEle mentById("tb");
for (var i=0; i<T.rows.length ; i++) {
alert(T.rows[i]);
}
I guess it's getElementsByTa gName. I'm addicted to white space.
Both table elements and row-group elements (thead, tbody and tfoot)
have a "rows" collection of the table rows inside them. And it's
W3C DOM compliant *and* works in IE, so no reason not to use it.
I know. The full code will replace a (specified) table cell, which would
have been much prettier just using the childNodes. The full code isn't
as easy to see the problem with as this, though it not huge or anything.


Table rows also have a "cells" collection, so you can access row i and
column j as:
T.rows[i].cells[j]
Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
In article <oe**********@h otpop.com>, lr*@hotpop.com enlightened us
with...

Table rows also have a "cells" collection, so you can access row i and
column j as:
T.rows[i].cells[j]


I tried that before and it didn't work in Netscape. Worked fine in IE.
But that was due to my original syntax, which referenced the table, not
the tbody.
var T = document.getEle mentById("t1"); // the table
alert(T.firstCh ild.rows[i].cells[j].innerHTML); // firstChild
expected to reference tbody, doesn't with the white space issue in NN

Changing it to reference the tbody instead works in both.
var T = document.getEle mentById("t1");
alert(T.firstCh ild.rows[i].cells[j].innerHTML);

On this whole issue, I'm preferring MSIE. I've gotten this to work at
least 7 different ways in IE and only 1 (well, 2 now) in NN.

Thanks for the alternative, though.
I think I like it better than what I ended up with...

--
--
~kaeli~
With her marriage, she got a new name and a dress.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #5
In article <oe**********@h otpop.com>, lr*@hotpop.com enlightened us
with...

Table rows also have a "cells" collection, so you can access row i and
column j as:
T.rows[i].cells[j]


Oh, before I go changing things from getElementsByTa gName...
Is the rows/cells thing as cross-browser as getElementsByTa gName (if you
know)?
--
--
~kaeli~
If at first you don't succeed, skydiving is not for you.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6


kaeli wrote:
In article <oe**********@h otpop.com>, lr*@hotpop.com enlightened us
with...
Table rows also have a "cells" collection, so you can access row i and
column j as:
T.rows[i].cells[j]

Oh, before I go changing things from getElementsByTa gName...
Is the rows/cells thing as cross-browser as getElementsByTa gName (if you
know)?


I think so (I am sure for Mozilla, Netscape 6/7, Opera 7 although I
can't give you details for Konqueror and Safari now off the top of my
head) but as the W3C DOM Level 2 HTML module in
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-64060425
specifies the properties
rows
tHead
tFoot
tBodies
for <table> elements and the
rows
property for table section elements (<thead>, <tbody>, <tfoot>) as well
as the
cells
property for table cells elements (<th>, <td>) any modern browser needs
to implement that.
And IE's object model knows these properties too since IE 4 I think so
browsers like Safari and Konqueror have two reasons to implement them,
W3C DOM compatibility and IE DOM compatibility.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #7
In article <40********@ola f.komtel.net>, ma*******@yahoo .de enlightened
us with...
but as the W3C DOM Level 2 HTML module in
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-64060425
specifies the properties
rows
tHead
tFoot
tBodies
for <table> elements and the
rows
property for table section elements (<thead>, <tbody>, <tfoot>) as well
as the
cells
property for table cells elements (<th>, <td>) any modern browser needs
to implement that.

Wonderful!!

Thanks for the help.
--
--
~kaeli~
Does the name Pavlov ring a bell?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #8
kaeli <ti******@NOSPA M.comcast.net> wrote in message news:<MP******* *************** **@nntp.lucent. com>...
Can anyone explain this to me? It's driving me insane.

Save this and run it in IE or Opera and then in Mozilla or Netscape 6+.
In IE/Opera, I get the expected 4 alerts.
In Mozilla/Netscape, I get *9*.


There are various kinds of nodes in the W3C DOM: Element is only
one of them. Every tag in your HTML document results in an ELEMENT_NODE.
But the text between the tags is a TEXT_NODE (sometimes more than one)
and there are other kinds too.

Mozilla includes the TEXT_NODEs in the DOM that you see. The extras
in your example are your whitespace, e.g., between <tbody> and <tr>,
and between </tr> and <tr>. Another place you might need to be aware
of this is if you have an event object for a Click event, and you
look at the attribute that shows what node the event happened to.
If you click on text in your page, Mozilla will show that the
event's target is the TEXT node, while IE will show it as the
ELEMENT node (named P, TD, etc) that contains the text (because
IE has no TEXT nodes).
Jul 23 '05 #9
Ivo
"Martin Honnen" wrote
IE ignores those white space text nodes which most authors insert to
have their markup more readable e.g. you have written
<tbody>
<tr>
and not
<tbody><tr>
If you wrote all markup without white space between tags Mozilla, Opera
and IE would give you the same result for the child nodes.


You can also run this function (with the root html element as starting node)
onload:

function noemptytextnode s(node) {
// removes empty text nodes so DOMs get crossbrowser
for (var x = 0; x < node.childNodes .length; x++) {
var child = node.childNodes[x];
if ((child.nodeTyp e == 3)&&(!/\S/.test(child.nod eValue))) {
node.removeChil d(node.childNod es[x]);
x--;
} else if (child.nodeType == 1) {
noemptytextnode s(child);
}
}
}

HTH
Ivo
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3077
by: Ingmund Sjåstad | last post by:
Trying to make a dropdown menu. I working nice in IE6 but when I try a link in Netscape 7 nothing happens. Can anybody help me? <html> <head> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>JS</title> </head>
1
2527
by: Prasanna | last post by:
Friends, the Javascript function firstChild() works as expected in IE but returns "undefined" in Netscape 7.1 Here is the sample html code that I used. I am asking the javascript to print out the id of the child of para1. IE correctly prints the string "italicsid", but Netscape gives me an "undefined". Why so? Any help is greatly...
16
2393
by: Ben | last post by:
I have a page (www.eastex.net/ben/NewETN/index3.asp) that displays OK in IE, but not in Netscape. I used a combination of padding-top and height attributes to get a total height on four cells. Netscape seems to not respect the padding-top, and only goes by the height, resulting in the cells being too short. What options do I have in CSS? ...
7
4715
by: Jonas Smithson | last post by:
Hello all, I have an absolute positioned parent div, and nested inside it is an absolute positioned child div -- I mean the div *code* is nested inside, but the child is physically positioned so that it's completely outside the parent div (to the left of its parent, in a left-side margin area). The child div contains hyperlinks. Explorer...
1
3069
by: Hazz | last post by:
I have 5 tables in SQL Server. Each with the following design and a sample chain of the relationships from the root (WRL - World) UUS is the 'Code' of the first table and it is the 'Parent' value of the second table, etc. Parent varchar 3 Name varchar 60 Code varchar 3 WRL United States UUS <- UUS California UCA <- UCA North Coast UNC...
2
1364
by: Guadala Harry | last post by:
I need to show a group of nodes - arranged in a hierarchy - in a Web page. A simple arrangement of boxes containing text with connecting lines among related boxes would be fine. The nodes and their relationships are stored in a database. How could I generate a graphic that depicts the current nodes and relationships? I'd appreciate any...
1
1919
by: treeman | last post by:
I copied the example Compile is successful .net 2003 but i press button , nothing to show but the example has box show up, why ? where 's wrong?
0
2059
by: Treeman | last post by:
I copied the example Compile is successful .net 2003 but i press button , nothing to show but the example has box show up, why ? where 's wrong?
4
6476
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method return the total number of <foldernodes (9) regardless of calling from the <parent1> and <parent2node. XML: ----------------------------------
0
7644
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8085
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.