...

View Full Version : Table innerHTML making a <tbody>?



itsjareds
06-22-2008, 09:43 PM
Hi, I am using greasemonkey to edit a page, and the site does not use <tbody> within its tables.

This is the table I want to change:

<table><tr><td><form ... ><input ... ><input ... ></form></td></tr></table>

When I checked the innerHTML of one of the tables, it gave me:

<table><tbody><td><tr><form ... ></form><input ... ><input ... ></td></tr></tbody></table>

There are two things wrong: I never put in a <tbody>, and it closes the form right when the tag is opened.

Is there any way to get JUST what is within the HTML, without generating (incorrect) code?

mrhoo
06-22-2008, 11:56 PM
innerHTML reports the html after it is read by the browser.

innerHTML is almost never the same as the literal text, and it is not likely to be the same in two different browsers.

IE reads html tags and many attributes in uppercase, and it will leave out quotation marks in some of your attributes, and an occasional closing tag.
If the browser does not read a tag like tbody, that is 'optional' in the html dtd it will insert it where it expects it to be.

So which is more important to know- the code you wrote, or the code the browser is using to render the page?

Kor
06-23-2008, 09:28 AM
Hi, I am using greasemonkey to edit a page, and the site does not use <tbody> within its tables.

This is the table I want to change:
There are two things wrong: I never put in a <tbody>, and it closes the form right when the tag is opened.

Is there any way to get JUST what is within the HTML, without generating (incorrect) code?
On the contrary, this is the correct generated code. Even if in HTML tbody is not required, in Javascript it is, thus any browser in this world will show you the TBODY element as present in a TABLE, no matter you have physically written it or not. Further more, if you want to use DOM methods to create/remove table's rows, you must consider the tbody as present and take that tbody as the parent of the rows.
.

Arbitrator
06-23-2008, 09:31 AM
There are two things wrong: I never put in a <tbody>, and it closes the form right when the tag is opened.The tbody element has optional start and end tags. Elements like that are essentially implied and inserted into the DOM automatically by the browser when needed. Essentially, all table elements must contain a tbody element unless all of its tr elements are contained within thead and tfoot elements; the latter situation would almost certainly be an incorrect use of an HTML table, so, effectively, that means that all correctly written HTML tables have at least one tbody element.

If it helps, consider that tr elements are invalid when placed as children of table elements. Only col, colgroup, del, ins, tbody, tfoot, and thead elements may be children of a table element. The following DOM representation is incorrect per the HTML 4.01 DTD. (The code is valid source code but not correct output.)


<table>
<tr>
<td></td>
</tr>
</table>

The browser fixes it by inserting implied tags.


<table>
<tbody><tr>
<td></td>
</tr>
</tbody></table>

You have the exact same situation with other implied tags as shown in the following example (comprised of correctly written source code).


<!doctype html public "-//W3C//DTD HTML 4.01//EN">
<title></title>
<p><div></div>
<p><var></var>
</body>

The resulting code would be the following.


<!doctype html public "-//W3C//DTD HTML 4.01//EN">
<html><head><title></title>
</head><body><p></p><div></div>
<p><var></var>
</p></body></html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum