Writing table markup with script

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I have some markup like the following:

<form>
<table>
<script>
<!-- Write the table markup //-->
</script>
</table>
<form>

The script draws the guts of the table as it is being rendered. This
works just fine, except that HTML 4.01 strict (to which I'd like to
conform) requires <script> tags to be children of either <head> or
<body>. I'd like to find a good way to get the <script> where the
standard wants it. My first idea is to draw the table with script
using innerHTML after the table has rendered, but the table in
question is decently sized, which makes me suspect that a browser
would have some performance issues with rendering it after the fact.
Is there a better way to accomplish what I want?
 
M

Michael Winter

On Fri, 1 Oct 2004 19:42:35 +0000 (UTC), Christopher Benson-Manica

[snip]
The script draws the guts of the table as it is being rendered.

I'll assume that using server-side scripting is not an option.
This works just fine, except that HTML 4.01 strict (to which I'd like to
conform) requires <script> tags to be children of either <head> or
<body>.

That's not quite true. SCRIPT elements can be placed in the HEAD element,
and anywhere else that accepts "special" or "inline" elements. That
includes many places, but it does not include SCRIPTs as direct children
of TABLEs. In fact, only a few elements can be direct children of any
TABLE element.

[snip]
Is there a better way to accomplish what I want?

Well, what precisely do you want? Are you writing the entire table, or
just a subsection of it? If it's the latter, are we talking an entire
table section, a couple of rows, or just one or two cells?

If you're writing the entire table, write the TABLE tags with the script,
too. If it is just a few parts, it would be best to add them after the
table has been rendered.

By the way, you can only write to the innerHTML property of TD elements.
All other table components (expect CAPTION) are read-only. However, you
can use the W3C DOM to add, remove and modify table sections and rows.

Mike
 
C

Christopher Benson-Manica

Michael Winter said:
I'll assume that using server-side scripting is not an option.
Correct.

That's not quite true. SCRIPT elements can be placed in the HEAD element,
and anywhere else that accepts "special" or "inline" elements. That
includes many places, but it does not include SCRIPTs as direct children
of TABLEs. In fact, only a few elements can be direct children of any
TABLE element.

The W3C site http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1
suggests, at least to me, that <head> and <body> are the only
permissible parents of said:
Well, what precisely do you want? Are you writing the entire table, or
just a subsection of it? If it's the latter, are we talking an entire
table section, a couple of rows, or just one or two cells?

It's the entire table, a typical size of which is 6 rows by 10
columns. It seems to my uneducated thinking that that's a significant
amount of work for a browser to insert using innerHTML.
If you're writing the entire table, write the TABLE tags with the script,
too. If it is just a few parts, it would be best to add them after the
table has been rendered.

Well, currently I *am* writing the table with script, but if my
interpretation of the information I linked to above is correct, I
won't be able to do so and conform to the HTML 4.01 standard.
By the way, you can only write to the innerHTML property of TD elements.
All other table components (expect CAPTION) are read-only. However, you
can use the W3C DOM to add, remove and modify table sections and rows.

But I could insert an entire table in a <td>, correct? It does happen
that in the real markup the table in question is the child of a <td>
element. Perhaps that option is at least possible...
 
M

Michael Winter

On Fri, 1 Oct 2004 20:05:08 +0000 (UTC), Christopher Benson-Manica

[snip]
The W3C site http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1
suggests, at least to me, that <head> and <body> are the only
permissible parents of <script>.

True, the text of the specification states that SCRIPT elements may appear
in the HEAD and BODY elements, but it doesn't say that is the only
location. The DTD is more specific:

<!ENTITY % special
"A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">

marks the SCRIPT element as a special entity, which in turn,

<!ENTITY % inline
"#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">

is an inline element.


<!ELEMENT HEAD O O (%head.content;) +(%head.misc;)>

and

<!ENTITY % head.misc "SCRIPT|STYLE|META|LINK|OBJECT">

declare that the SCRIPT element may occur in the HEAD element any number
of times.

Furthermore,

<!ELEMENT BODY O O (%block;|SCRIPT)+ +(INS|DEL)>

makes a special exception for SCRIPT, which unlike other inline elements,
may appear as a direct child of the BODY element.

So far, this agrees with your assessment, but:

<!ELEMENT P - O (%inline;)*>

and

<!ELEMENT A - - (%inline;)* -(A)>

do not. Here, both P and A elements state that they may contain any inline
elements. If SCRIPTs were disallowed, a similar entry would occur like
"-(A)" to explicitly disallow them. The vast majority of non-empty
elements are the same. The only exceptions are elements like lists,
tables, SELECTs, and similar that define precisely what they can contain.

I hope that's a clear enough case. :)
It's the entire table, a typical size of which is 6 rows by 10 columns.
It seems to my uneducated thinking that that's a significant amount of
work for a browser to insert using innerHTML.

I don't think so. Richard Cornford (<URL:http://www.litotes.demon.co.uk/>)
has written a table sorter that obviously requires the movement of
potentially all the rows in a table. His demo initially uses 200 rows and
the operation occurs very quickly. What you're proposing isn't that taxing
for modern processors. Can't say I know when performance would suffer,
though.

[snip]
Well, currently I *am* writing the table with script, but if my
interpretation of the information I linked to above is correct, I won't
be able to do so and conform to the HTML 4.01 standard.

You can, but the SCRIPT element must be placed in an element that allows
inline elements. P, TD, DIV, and numerous others all qualify.

[snip]
But I could insert an entire table [using innerHTML] in a <td>,
correct? It does happen that in the real markup the table in question
is the child of a <td> element. Perhaps that option is at least
possible...

You could. It would probably be more efficient than using the W3C DOM,
too, unless the rows were identical and you could just clone them.

Mike
 
C

Christopher Benson-Manica

David Dorward said:
"in" means "descendent of" not "child of"

Oh, now I see. So Michael's original suggestion was spot on. Thanks
to both of you.
 
C

Christopher Benson-Manica

Michael Winter said:
I hope that's a clear enough case. :)

Yes, I did understand it... maybe next time I will look at the DTD
myself before making guesses. ;)
You could. It would probably be more efficient than using the W3C DOM,
too, unless the rows were identical and you could just clone them.

Yes, now I see, and I have done so. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top