By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Writing table markup with script

P: n/a
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, 1 Oct 2004 19:42:35 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
Michael Winter <M.******@blueyonder.co.invalid> spoke thus:
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/....html#h-18.2.1
suggests, at least to me, that <head> and <body> are the only
permissible parents of <script>.
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...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #3

P: n/a
Christopher Benson-Manica wrote:
The W3C site http://www.w3.org/TR/html4/interact/....html#h-18.2.1
suggests, at least to me, that <head> and <body> are the only
permissible parents of <script>.


"in" means "descendent of" not "child of"

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #4

P: n/a
On Fri, 1 Oct 2004 20:05:08 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:

[snip]
The W3C site http://www.w3.org/TR/html4/interact/....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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

P: n/a
David Dorward <do*****@yahoo.com> spoke thus:
"in" means "descendent of" not "child of"


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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #6

P: n/a
Michael Winter <M.******@blueyonder.co.invalid> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.