Writing table markup with script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Writing table markup with script

    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)cybers pace.org | don't, I need to know. Flames welcome.
  • Michael Winter

    #2
    Re: Writing table markup with script

    On Fri, 1 Oct 2004 19:42:35 +0000 (UTC), Christopher Benson-Manica
    <ataru@nospam.c yberspace.org> wrote:

    [snip]
    [color=blue]
    > The script draws the guts of the table as it is being rendered.[/color]

    I'll assume that using server-side scripting is not an option.
    [color=blue]
    > 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>.[/color]

    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]
    [color=blue]
    > Is there a better way to accomplish what I want?[/color]

    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.

    Comment

    • Christopher Benson-Manica

      #3
      Re: Writing table markup with script

      Michael Winter <M.Winter@bluey onder.co.invali d> spoke thus:
      [color=blue]
      > I'll assume that using server-side scripting is not an option.[/color]

      Correct.
      [color=blue]
      > 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.[/color]

      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>.
      [color=blue]
      > 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?[/color]

      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.
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > 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.[/color]

      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)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • David Dorward

        #4
        Re: Writing table markup with script

        Christopher Benson-Manica wrote:
        [color=blue]
        > 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>.[/color]

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

        --
        David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
        Home is where the ~/.bashrc is

        Comment

        • Michael Winter

          #5
          Re: Writing table markup with script

          On Fri, 1 Oct 2004 20:05:08 +0000 (UTC), Christopher Benson-Manica
          <ataru@nospam.c yberspace.org> wrote:

          [snip]
          [color=blue]
          > 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>.[/color]

          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|M ETA|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. :)
          [color=blue]
          > 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.[/color]

          I don't think so. Richard Cornford (<URL:http://www.litotes.dem on.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]
          [color=blue]
          > 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.[/color]

          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]
          [color=blue]
          > 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...[/color]

          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.

          Comment

          • Christopher Benson-Manica

            #6
            Re: Writing table markup with script

            David Dorward <dorward@yahoo. com> spoke thus:
            [color=blue]
            > "in" means "descendent of" not "child of"[/color]

            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)cybers pace.org | don't, I need to know. Flames welcome.

            Comment

            • Christopher Benson-Manica

              #7
              Re: Writing table markup with script

              Michael Winter <M.Winter@bluey onder.co.invali d> spoke thus:
              [color=blue]
              > I hope that's a clear enough case. :)[/color]

              Yes, I did understand it... maybe next time I will look at the DTD
              myself before making guesses. ;)
              [color=blue]
              > 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.[/color]

              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)cybers pace.org | don't, I need to know. Flames welcome.

              Comment

              Working...