Inserting Tables in IE 6 using W3C DOM techniques.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mellow Crow

    Inserting Tables in IE 6 using W3C DOM techniques.

    I had a problem in IE 6 when trying to insert a table using W3C DOM
    techniques.

    I found a solution and share it. :)

    Initially I had......

    *************** *******
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="content-script-type" content="text/javascript" />

    <title>insertTa bleInIE</title>

    <script type="text/javascript">
    /* <![CDATA[ */

    function insertTable() {
    var table = document.create Element("table" );
    table.setAttrib ute("id","table 2");
    var tr = document.create Element("tr");
    var td = document.create Element("td");
    td.appendChild( document.create TextNode("Some stuff in the cell"));
    tr.appendChild( td);
    table.appendChi ld(tr);
    document.getEle mentById('outpu t').appendChild (table);
    }

    /* ]]> */
    </script>
    </head>
    <body>
    <div>
    <input type="button" id="btnInsertTa ble" name="btnInsert Table"
    onclick="insert Table()" value="Insert Table" />
    </div>

    <div id="output"></div>
    </body>
    </html>
    *************** *************
    This would work in firefox 1.5 but not in IE 6.

    The solution.... you have to explicitly insert the tr into a tbody element
    and then the tbody into the table. So the function now becomes:

    function insertTable() {
    var table = document.create Element("table" );
    table.setAttrib ute("id","table 2");
    var tbody = document.create Element("tbody" ); // explicitly create a
    tbody
    var tr = document.create Element("tr");
    var td = document.create Element("td");
    td.appendChild( document.create TextNode("Some stuff in the cell"));
    tr.appendChild( td);
    tbody.appendChi ld(tr); // note this new line.
    table.appendChi ld(tbody); // append tbody rather than tr
    document.getEle mentById('outpu t').appendChild (table);
    }

    Hope this saves some else an hour or two.



  • Ian

    #2
    Re: Inserting Tables in IE 6 using W3C DOM techniques.

    Mellow Crow wrote:[color=blue]
    > I had a problem in IE 6 when trying to insert a table using W3C DOM
    > techniques.
    >
    > I found a solution and share it. :)
    >[/color]
    Nice, looks like IE is doing the correct thing, table rows should be
    contained in a tbody.

    The HTML-DOM interface insertRow should add this for you if it isn't there.

    Ian

    Comment

    • Patient Guy

      #3
      Re: Inserting Tables in IE 6 using W3C DOM techniques.

      "Mellow Crow" <noemail@nowher e.com> wrote in
      news:43a77312$1 @duster.adelaid e.on.net:
      [color=blue]
      > I had a problem in IE 6 when trying to insert a table using W3C DOM
      > techniques.
      >
      > I found a solution and share it. :)
      >
      > Initially I had......
      >
      > *************** *******
      > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
      > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      > <head>
      > <meta http-equiv="content-type" content="text/html; charset=utf-8"
      > /> <meta http-equiv="content-script-type" content="text/javascript"
      > />
      >
      > <title>insertTa bleInIE</title>
      >
      > <script type="text/javascript">
      > /* <![CDATA[ */
      >
      > function insertTable() {
      > var table = document.create Element("table" );
      > table.setAttrib ute("id","table 2");
      > var tr = document.create Element("tr");
      > var td = document.create Element("td");
      > td.appendChild( document.create TextNode("Some stuff in the
      > cell")); tr.appendChild( td);
      > table.appendChi ld(tr);
      > document.getEle mentById('outpu t').appendChild (table);
      > }
      >
      > /* ]]> */
      > </script>
      > </head>
      > <body>
      > <div>
      > <input type="button" id="btnInsertTa ble" name="btnInsert Table"
      > onclick="insert Table()" value="Insert Table" />
      > </div>
      >
      > <div id="output"></div>
      > </body>
      > </html>
      > *************** *************
      > This would work in firefox 1.5 but not in IE 6.
      >
      > The solution.... you have to explicitly insert the tr into a tbody
      > element and then the tbody into the table. So the function now
      > becomes:
      >
      > function insertTable() {
      > var table = document.create Element("table" );
      > table.setAttrib ute("id","table 2");
      > var tbody = document.create Element("tbody" ); // explicitly
      > create a
      > tbody
      > var tr = document.create Element("tr");
      > var td = document.create Element("td");
      > td.appendChild( document.create TextNode("Some stuff in the
      > cell")); tr.appendChild( td);
      > tbody.appendChi ld(tr); // note this new line.
      > table.appendChi ld(tbody); // append tbody rather than tr
      > document.getEle mentById('outpu t').appendChild (table);
      > }
      >
      > Hope this saves some else an hour or two.[/color]

      Since table rows are also part of table headers ('thead') and table
      footers ('tfoot'), are you saying that IE refers to create child table row
      elements for these nodes?

      As long as you're into experiments, then here might be some more
      experiments for you:

      * See how the browsers do in adding MORE THAN ONE table header or table
      footer element to a table node through calling DOM functions. Try more
      than one table body while you're at it.

      * Write more than one table header, table footer, and table body into a
      table using validated HTML, see how the browser parses it by inspecting
      the DOM tree thereafter. (Alternatively report the exceptions you find in
      attempting certain calls.)

      * Compose a validating simple HTML document in which you create a table
      WITHOUT any section elements---no THEAD, no TBODY, no TFOOT----and just a
      couple of rows each with a couple of cells (can be void of content).
      Inspect the DOM hierarchy to see which browsers imposed section elements
      (namely TBODY) into the table, and which rendered it just as you wrote it,
      and which conformed to all specifications (HTML, ECMA-262::Javascript ,
      DOM).


      The results are a curiosity.



      Comment

      • Patient Guy

        #4
        Re: Inserting Tables in IE 6 using W3C DOM techniques.

        Patient Guy <Patient.Guy@no where.to.be.fou nd.com> wrote in
        news:Xns973258C 9A4957UVAA@207. 115.17.102:
        [color=blue]
        > "Mellow Crow" <noemail@nowher e.com> wrote in
        > news:43a77312$1 @duster.adelaid e.on.net:
        >[color=green]
        >> I had a problem in IE 6 when trying to insert a table using W3C DOM
        >> techniques.
        >>
        >> I found a solution and share it. :)
        >>
        >> Initially I had......
        >>
        >> *************** *******
        >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        >> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
        >> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        >> <head>
        >> <meta http-equiv="content-type" content="text/html; charset=utf-8"
        >> /> <meta http-equiv="content-script-type" content="text/javascript"
        >> />
        >>
        >> <title>insertTa bleInIE</title>
        >>
        >> <script type="text/javascript">
        >> /* <![CDATA[ */
        >>
        >> function insertTable() {
        >> var table = document.create Element("table" );
        >> table.setAttrib ute("id","table 2");
        >> var tr = document.create Element("tr");
        >> var td = document.create Element("td");
        >> td.appendChild( document.create TextNode("Some stuff in the
        >> cell")); tr.appendChild( td);
        >> table.appendChi ld(tr);
        >> document.getEle mentById('outpu t').appendChild (table);
        >> }
        >>
        >> /* ]]> */
        >> </script>
        >> </head>
        >> <body>
        >> <div>
        >> <input type="button" id="btnInsertTa ble" name="btnInsert Table"
        >> onclick="insert Table()" value="Insert Table" />
        >> </div>
        >>
        >> <div id="output"></div>
        >> </body>
        >> </html>
        >> *************** *************
        >> This would work in firefox 1.5 but not in IE 6.
        >>
        >> The solution.... you have to explicitly insert the tr into a tbody
        >> element and then the tbody into the table. So the function now
        >> becomes:
        >>
        >> function insertTable() {
        >> var table = document.create Element("table" );
        >> table.setAttrib ute("id","table 2");
        >> var tbody = document.create Element("tbody" ); // explicitly
        >> create a
        >> tbody
        >> var tr = document.create Element("tr");
        >> var td = document.create Element("td");
        >> td.appendChild( document.create TextNode("Some stuff in the
        >> cell")); tr.appendChild( td);
        >> tbody.appendChi ld(tr); // note this new line.
        >> table.appendChi ld(tbody); // append tbody rather than tr
        >> document.getEle mentById('outpu t').appendChild (table);
        >> }
        >>
        >> Hope this saves some else an hour or two.[/color]
        >
        > Since table rows are also part of table headers ('thead') and table
        > footers ('tfoot'), are you saying that IE refers to create child table
        > row elements for these nodes?[/color]

        Proofreading Correction:

        "...are you saying that IE refers to create..."

        should read

        "...are you saying that IE REFUSES to create..."
        [color=blue]
        > As long as you're into experiments, then here might be some more
        > experiments for you:
        >
        > * See how the browsers do in adding MORE THAN ONE table header or
        > table footer element to a table node through calling DOM functions.
        > Try more than one table body while you're at it.
        >
        > * Write more than one table header, table footer, and table body into
        > a table using validated HTML, see how the browser parses it by
        > inspecting the DOM tree thereafter. (Alternatively report the
        > exceptions you find in attempting certain calls.)
        >
        > * Compose a validating simple HTML document in which you create a
        > table WITHOUT any section elements---no THEAD, no TBODY, no
        > TFOOT----and just a couple of rows each with a couple of cells (can be
        > void of content). Inspect the DOM hierarchy to see which browsers
        > imposed section elements (namely TBODY) into the table, and which
        > rendered it just as you wrote it, and which conformed to all
        > specifications (HTML, ECMA-262::Javascript , DOM).
        >
        >
        > The results are a curiosity.
        >
        >
        >
        >[/color]



        Comment

        • Tony

          #5
          Re: Inserting Tables in IE 6 using W3C DOM techniques.

          Ian wrote:[color=blue]
          > Mellow Crow wrote:[color=green]
          > > I had a problem in IE 6 when trying to insert a table using W3C DOM
          > > techniques.
          > >
          > > I found a solution and share it. :)
          > >[/color]
          > Nice, looks like IE is doing the correct thing, table rows should be
          > contained in a tbody.
          >
          > The HTML-DOM interface insertRow should add this for you if it isn't there.[/color]

          I've discovered that the big difference is: IE inserts rows at the END
          of the table, after the existing rows. Firefox inserts rows at the
          BEGINNING of the table, before the existing rows.

          If you have a <thead> and a <tbody> defined, then Firefox will insert
          rows at the beginning of the <tbody>. Otherwise, it assumes that the
          entire table is a <tbody> and inserts at the top.

          Not sure which is the "proper" way to do it, but in this case, I'd say
          the IE way makes more sense - when I want to add a row to an existing
          table, I generally want it at the END.

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Inserting Tables in IE 6 using W3C DOM techniques.

            Tony wrote:
            [color=blue]
            > Ian wrote:[color=green]
            >> The HTML-DOM interface insertRow should add this for you if it isn't
            >> there.[/color]
            >
            > I've discovered that the big difference is: IE inserts rows at the END
            > of the table, after the existing rows. Firefox inserts rows at the
            > BEGINNING of the table, before the existing rows.
            >
            > If you have a <thead> and a <tbody> defined, then Firefox will insert
            > rows at the beginning of the <tbody>. Otherwise, it assumes that the
            > entire table is a <tbody> and inserts at the top.
            >
            > Not sure which is the "proper" way to do it, [...][/color]

            That depends on how you call insertRow().

            <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>


            PointedEars

            Comment

            • VK

              #7
              Re: Inserting Tables in IE 6 using W3C DOM techniques.


              Tony wrote:[color=blue]
              > I've discovered that the big difference is: IE inserts rows at the END
              > of the table, after the existing rows. Firefox inserts rows at the
              > BEGINNING of the table, before the existing rows.[/color]

              That's an implementation bug in some Mozilla browsers and Safari. It
              was explained here:
              <http://groups.google.c om/group/comp.lang.javas cript/browse_frm/thread/f1d06d29d2fd415 e/0ba38e911abbbcb 2>

              As I said it is easy to fix for all browsers by using optional rowIndex
              argument:
              insertRow(-1)
              [color=blue]
              > If you have a <thead> and a <tbody> defined, then Firefox will insert
              > rows at the beginning of the <tbody>. Otherwise, it assumes that the
              > entire table is a <tbody> and inserts at the top.
              >
              > Not sure which is the "proper" way to do it, but in this case, I'd say
              > the IE way makes more sense - when I want to add a row to an existing
              > table, I generally want it at the END.[/color]

              Naturally. This is why it's a rather silly implementation bug.

              Also note that IE will create tbody section for you automatically - but
              not thead or tfoot. Some browsers will not do event that. The rule is:
              if you plan to script your table later, mark up all three sections in
              advance:
              <table>
              <thead></thead>
              <tfoot></thead>
              <tbody>
              ....
              </tbody>
              </table>

              Comment

              • VK

                #8
                Re: Inserting Tables in IE 6 using W3C DOM techniques.

                <table>
                <thead></thead>
                <tfoot></foot>
                <tbody>
                ....
                </tbody>
                </table>

                Comment

                • VK

                  #9
                  Re: Inserting Tables in IE 6 using W3C DOM techniques.

                  And the 3rd final attempt to make it right :-)

                  <table>
                  <thead></thead>
                  <tfoot></tfoot>
                  <tbody>
                  ....
                  </tbody>
                  </table>

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Inserting Tables in IE 6 using W3C DOM techniques.

                    VK wrote:
                    [color=blue]
                    > Tony wrote:[color=green]
                    >> I've discovered that the big difference is: IE inserts rows at the END
                    >> of the table, after the existing rows. Firefox inserts rows at the
                    >> BEGINNING of the table, before the existing rows.[/color]
                    >
                    > That's an implementation bug in some Mozilla browsers and Safari.
                    > It was explained here:
                    >[/color]
                    <http://groups.google.c om/group/comp.lang.javas cript/browse_frm/thread/f1d06d29d2fd415 e/0ba38e911abbbcb 2>[color=blue]
                    >
                    > As I said it is easy to fix for all browsers by using optional rowIndex
                    > argument:
                    > insertRow(-1)[/color]

                    Since the HTMLTableElemen t::insertRow() method of W3C DOM Level 2 HTML
                    _requires_ that argument, that is, it is _not_ optional, how could
                    unexpected behavior due to omission of that argument possibly be
                    considered an "implementa tion bug"?

                    You are talking nonsense again.


                    PointedEars

                    Comment

                    • VK

                      #11
                      Re: Inserting Tables in IE 6 using W3C DOM techniques.


                      Thomas 'PointedEars' Lahn wrote:[color=blue]
                      > Since the HTMLTableElemen t::insertRow() method of W3C DOM Level 2 HTML
                      > _requires_ that argument, that is, it is _not_ optional, how could
                      > unexpected behavior due to omission of that argument possibly be
                      > considered an "implementa tion bug"?[/color]

                      Because insertRow() method was finally monkey-out from the Microsoft
                      model where
                      insertRow() has *optional* argument rowIndex set by default to *-1*
                      (new row goes to the bottom of the indicated table section). That was
                      perfectly logical and convenient. But as soon as it got into hands of
                      W3C it lost any logic and convenience (as usual).

                      We make organize a surway across of developers to ask what behavior
                      they see as expected and more convenient. I accept bets 100:1 for
                      results.

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: Inserting Tables in IE 6 using W3C DOM techniques.

                        VK wrote:
                        [color=blue]
                        > Thomas 'PointedEars' Lahn wrote:[color=green]
                        >> Since the HTMLTableElemen t::insertRow() method of W3C DOM Level 2 HTML
                        >> _requires_ that argument, that is, it is _not_ optional, how could
                        >> unexpected behavior due to omission of that argument possibly be
                        >> considered an "implementa tion bug"?[/color]
                        >
                        > Because insertRow() method was finally monkey-out from the Microsoft
                        > model [...][/color]

                        You have yet to prove that.
                        [color=blue]
                        > where insertRow() has *optional* argument rowIndex set by default to *-1*
                        > [...][/color]

                        So? The Gecko DOM does not implement the IE DOM. That is just more
                        nonsense from you.


                        PointedEars

                        Comment

                        Working...