Problem with document.write("<td>Hello</td>")

Discussion in 'Javascript' started by bissatch@yahoo.co.uk, Jun 1, 2005.

  1. Guest

    Hi,

    I am trying to use JavaScript to write a table column on a web page.

    The code is as follows:

    <html>
    <head>
    <script>
    function displaycount() {
    document.write ("<td colspan="5">Hello</td>");
    }
    </script>
    </head>

    <body>
    <table border="0" cellpadding="0" cellspacing="0" width="315"
    id="g8clock">
    <tr>
    <td width="15"></td><td width="75">DAYS</td><td
    width="75">HRS</td><td width="75">MINS</td><td width="75">SECS</td>
    </tr>
    <tr>
    <script type="text/javascript">displaycount();</script>
    </tr>
    </table>
    </body>
    </html>

    As can be seen, there is a section on in the table that calls the
    function displaycount(). All this does is write the HTML to display a
    new column.

    For some reason, this doesnt work although. Can anybody tell me why
    this work write the table column. I know the code document.write code
    works because when I remove the function from inside the table and
    change the output to doument.write("<p>Hello</p>") it works. Any help?

    Cheers

    Burnsy
     
    , Jun 1, 2005
    #1
    1. Advertising

  2. Random Guest

    wrote:
    > Hi,
    >
    > I am trying to use JavaScript to write a table column on a web page.
    >
    > The code is as follows:
    >
    > <html>
    > <head>
    > <script>
    > function displaycount() {
    > document.write ("<td colspan="5">Hello</td>");
    > }
    > </script>
    > </head>
    >
    > <body>
    > <table border="0" cellpadding="0" cellspacing="0" width="315"
    > id="g8clock">
    > <tr>
    > <td width="15"></td><td width="75">DAYS</td><td
    > width="75">HRS</td><td width="75">MINS</td><td width="75">SECS</td>
    > </tr>
    > <tr>
    > <script type="text/javascript">displaycount();</script>
    > </tr>
    > </table>
    > </body>
    > </html>
    >
    > As can be seen, there is a section on in the table that calls the
    > function displaycount(). All this does is write the HTML to display a
    > new column.
    >
    > For some reason, this doesnt work although. Can anybody tell me why
    > this work write the table column. I know the code document.write code
    > works because when I remove the function from inside the table and
    > change the output to doument.write("<p>Hello</p>") it works. Any help?
    >
    > Cheers
    >
    > Burnsy



    Turn on error-checking in your browser.

    This line:
    document.write ("<td colspan="5">Hello</td>");

    is a syntax error because of the quotation marks.
    Any of the following will fix it:

    document.write ('<td colspan="5">Hello</td>');
    document.write ("<td colspan='5'>Hello</td>");
    document.write ("<td colspan=\"5\">Hello</td>");
    document.write ('<td colspan=\'5\'>Hello</td>');
    document.write ("<td colspan=5>Hello</td>");
    document.write ('<td colspan=5>Hello</td>');


    I recommend the first.
     
    Random, Jun 1, 2005
    #2
    1. Advertising

  3. Grant Wagner Guest

    "Random" <> wrote in message
    news:...
    > wrote:
    >> Hi,
    >>
    >> I am trying to use JavaScript to write a table column on a web page.
    >>
    >> The code is as follows:
    >>
    >> <html>
    >> <head>
    >> <script>
    >> function displaycount() {
    >> document.write ("<td colspan="5">Hello</td>");
    >> }
    >> </script>
    >> </head>
    >>
    >> <body>
    >> <table border="0" cellpadding="0" cellspacing="0" width="315"
    >> id="g8clock">
    >> <tr>
    >> <td width="15"></td><td width="75">DAYS</td><td
    >> width="75">HRS</td><td width="75">MINS</td><td width="75">SECS</td>
    >> </tr>
    >> <tr>
    >> <script type="text/javascript">displaycount();</script>
    >> </tr>
    >> </table>
    >> </body>
    >> </html>
    >>
    >> As can be seen, there is a section on in the table that calls the
    >> function displaycount(). All this does is write the HTML to display
    >> a
    >> new column.
    >>
    >> For some reason, this doesnt work although. Can anybody tell me why
    >> this work write the table column. I know the code document.write code
    >> works because when I remove the function from inside the table and
    >> change the output to doument.write("<p>Hello</p>") it works. Any
    >> help?
    >>
    >> Cheers
    >>
    >> Burnsy

    >
    >
    > Turn on error-checking in your browser.
    >
    > This line:
    > document.write ("<td colspan="5">Hello</td>");
    >
    > is a syntax error because of the quotation marks.
    > Any of the following will fix it:
    >
    > document.write ('<td colspan="5">Hello</td>');
    > document.write ("<td colspan='5'>Hello</td>");
    > document.write ("<td colspan=\"5\">Hello</td>");
    > document.write ('<td colspan=\'5\'>Hello</td>');
    > document.write ("<td colspan=5>Hello</td>");
    > document.write ('<td colspan=5>Hello</td>');
    >
    >
    > I recommend the first.


    I'd suggest not writing the table cell elements using JavaScript at all,
    if JavaScript is disabled or unavailable the page will contain invalid
    markup. You may also want to toss a space or &nbsp; into the cell, so if
    the JavaScript does not execute, the cell is not empty

    <script type="text/javascript">
    function displayCount() { document.write('Hello'); }
    </script>
    <table ...>
    ....
    <tr>
    <td colspan="5"><script
    type="text/javascript">displayCount();</script>&nbsp;</td>
    </tr>
    ....
    </table>

    A more "robust" solution might be something like the following. Although
    you're relying on more advanced features, any modern PC-based user agent
    that supports JavaScript and has JavaScript enabled is likely to support
    the functionality required to make it work. It depends on who your
    audience is of course.

    <script type="text/javascript">
    window.onload = function() {
    if (document.getElementById && document.createTextNode) {
    var count = document.getElementById('count');
    if (count && count.firstChild && count.replaceChild) {
    count.replaceChild(
    document.createTextNode('Hello'),
    count.firstChild
    );
    }
    }
    }
    </script>
    <table ...>
    ....
    <tr>
    <td colspan="5"><span id="count">Count not available in your
    browser</span></td>
    </tr>
    ....
    </table>

    Tested and working in IE 6.0.2900, Firefox 1.0.4, Mozilla 1.0.2, Mozilla
    1.7.8, Opera 8.0. Opera 7.54u2 gives it the old college try but doesn't
    quite get there.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Jun 1, 2005
    #3
    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. Warwick Boardman
    Replies:
    1
    Views:
    87
    Lasse Reichstein Nielsen
    Nov 20, 2003
  2. Gary Mayor
    Replies:
    0
    Views:
    253
    Gary Mayor
    Mar 3, 2004
  3. jullag
    Replies:
    20
    Views:
    338
    Frances
    Sep 22, 2005
  4. Martin Molema
    Replies:
    2
    Views:
    158
    dhtml
    Jan 11, 2009
  5. Frank Peterson
    Replies:
    9
    Views:
    244
    David Mark
    Jun 28, 2009
Loading...

Share This Page