JS in tables

Discussion in 'Javascript' started by U. Cortez - Research, Apr 6, 2006.

  1. I'm putting together some javascript code whose output needs to be
    formatted into two columns. I was trying to put it in an html table
    until someone alerted me that IE doesn't accept JS within tables. So
    I'm looking for workarounds or alternatives. I've Googled and have
    found plenty of similar questions, but no concrete solutions.

    Someone recommended just putting the table tags (table, tr, td) in
    document.write() functions, but this still yields the "Operation
    Aborted" error in IE.

    Any clues?

    -U.
    U. Cortez - Research, Apr 6, 2006
    #1
    1. Advertising

  2. U. Cortez - Research

    RobG Guest

    U. Cortez - Research said on 07/04/2006 5:24 AM AEST:
    > I'm putting together some javascript code whose output needs to be
    > formatted into two columns. I was trying to put it in an html table
    > until someone alerted me that IE doesn't accept JS within tables. So


    IE does 'accept JS in a table'. There are two table-related script
    issues with IE:

    1. You can't use innerHTML to modify the internal structure

    2. If createElement is used to create TR elements, they must
    be appended to a tbody element, not the table element.

    Other than that, it's business as usual. To modify a table, use DOM
    methods. Here's a link to the Mozilla DOM reference for tables:

    <URL:http://developer.mozilla.org/en/docs/DOM:table>


    If you use insertRow and insertCell to modify your table, you avoid both
    the above issues and you should have no problems with modern browsers
    (anything after Navigator/IE 4).

    Use feature detection to prevent errors in unsupported UAs.


    [...]

    > Someone recommended just putting the table tags (table, tr, td) in
    > document.write() functions, but this still yields the "Operation
    > Aborted" error in IE.


    You can use document.write to create all or part of a table while the
    page loads, but that is not a good idea in general.

    e.g.

    <table>
    <tr><td>top cell</td></tr>
    <script type='text/javascript'>
    document.write('<tr><td>bottom cell</td></tr>');
    </script>
    </table>


    Using document.write after the page has finished loading will cause the
    entire page to be re-written.


    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/FAQ>
    RobG, Apr 7, 2006
    #2
    1. Advertising

  3. RobG wrote:
    <snip>
    > You can use document.write to create all or part of a
    > table while the page loads, but that is not a good idea
    > in general.
    >
    > e.g.
    >
    > <table>
    > <tr><td>top cell</td></tr>
    > <script type='text/javascript'>
    > document.write('<tr><td>bottom cell</td></tr>');
    > </script>
    > </table>

    <snip>

    While in reality you can write this code you are opening yourself up to
    the vagaries of tag-soup HTML parsing and error correction as it is not
    valid HTML. A SCRIPT element may not be a child of a TABLE element, or
    the implied TBODY element, so the parser may error-correct the HTML to
    imply the closing of the TABLE and TBODY when it encounters the SCRIPT
    opening tag and then imply the opening of a new TABLE and TBODY when it
    encounters the document written opening TR tag. Then again it may make
    the whole lot into a single table. The variable and unpredictable
    outcome will likely make the scripted interacting with the resulting
    table(s) harder than it needs to be.

    Richard.
    Richard Cornford, Apr 7, 2006
    #3
  4. JRS: In article <>,
    dated Thu, 6 Apr 2006 12:24:35 remote, seen in
    news:comp.lang.javascript, U. Cortez - Research
    <> posted :
    >I'm putting together some javascript code whose output needs to be
    >formatted into two columns. I was trying to put it in an html table
    >until someone alerted me that IE doesn't accept JS within tables. So
    >I'm looking for workarounds or alternatives. I've Googled and have
    >found plenty of similar questions, but no concrete solutions.
    >
    >Someone recommended just putting the table tags (table, tr, td) in
    >document.write() functions, but this still yields the "Operation
    >Aborted" error in IE.
    >
    >Any clues?


    Without seeing a minimal demonstration of your actual code, or knowing
    which version of IE you are testing with, it's hard to know why it does
    not succeed. The following works for me as an HTML page :-

    <script>
    var Yr=1600, X=4, St
    B = "<\/th>\n<\/tr>\n<tr>\n<th>", C = "<\/th>\n<th>"
    St = "<table summary=\"T1\" border=1>\n" +
    "<caption>TABLE I<\/caption>\n<tr>\n<th>" +
    "6543210".split("").join(C) + B +
    "BCDEFGA".split("").join(C) + B +
    " ".split(" ").join(" <\/th>\n<th>") + " <\/th>\n"
    while (Yr<=8500) {
    St += "<td>" + Yr ; Yr += 100
    if (Yr%400==0) { St += "<br>" + Yr ; Yr += 100 }
    St += "<\/td>\n"
    X++ ; if (X==7) { X=0 ; St += "<\/tr>\n<tr>\n" } }
    St += "<td>&amp;c.<\/td>\n<td>" +
    " ".split(" ").join(" <\/td>\n<td>") +
    " <\/td>\n<\/tr>\n<\/table>"
    document.writeln(St)
    </script>

    However, it's certainly possible to amend an existing Table after
    loading.


    Read the newsgroup FAQ.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Apr 7, 2006
    #4
  5. Ok, to be more specific, I'm trying to insert a Google Map inside one
    cell of my table. However, if I do something like:

    <table>
    <tr><td>
    <script type="text/javascript">
    //google map code
    </script>
    </td></tr>
    </table>

    I get, "Internet Explorer cannot open the Internet site <location>.
    Operation aborted"

    -U.
    U. Cortez - Research, Apr 7, 2006
    #5
  6. > A SCRIPT element may not be a child of a TABLE element, or
    > the implied TBODY element


    Therein lies my problem. I need the output of the script to be
    displayed within the table. How can I work around this limitation in
    IE?

    -U.
    U. Cortez - Research, Apr 7, 2006
    #6
  7. U. Cortez - Research

    Evertjan. Guest

    U. Cortez - Research wrote on 07 apr 2006 in comp.lang.javascript:

    >> A SCRIPT element may not be a child of a TABLE element, or
    >> the implied TBODY element

    >
    > Therein lies my problem. I need the output of the script to be
    > displayed within the table. How can I work around this limitation in
    > IE?


    Use the DOM.

    Or wrie your whole HTML as a string to be document.written.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 7, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suzy

    newbie: tables/layout

    suzy, Jul 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    998
  2. Lou

    Hash Tables

    Lou, Sep 5, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    6,445
    Richard Grimes [MVP]
    Sep 29, 2003
  3. Peter Bassett
    Replies:
    3
    Views:
    913
    Augustus
    Aug 15, 2003
  4. Otuatail

    Tables within tables

    Otuatail, Jul 31, 2004, in forum: HTML
    Replies:
    7
    Views:
    483
  5. Chris Brat
    Replies:
    5
    Views:
    687
    =?iso-8859-1?q?Luis_M._Gonz=E1lez?=
    Aug 22, 2006
Loading...

Share This Page