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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bissatch@yahoo.co.uk

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

    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">Hel lo</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">dis playcount();</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

  • Random

    #2
    Re: Problem with document.write( &quot;&lt;td&gt ;Hello&lt;/td&gt;&quot;)

    bissatch@yahoo. co.uk wrote:[color=blue]
    > 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">Hel lo</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">dis playcount();</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[/color]


    Turn on error-checking in your browser.

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

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

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


    I recommend the first.

    Comment

    • Grant Wagner

      #3
      Re: Problem with document.write( &quot;&lt;td&gt ;Hello&lt;/td&gt;&quot;)

      "Random" <randomiez@gmai l.com> wrote in message
      news:1117624114 .532563.180600@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > bissatch@yahoo. co.uk wrote:[color=green]
      >> 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">Hel lo</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">dis playcount();</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[/color]
      >
      >
      > Turn on error-checking in your browser.
      >
      > This line:
      > document.write ("<td colspan="5">Hel lo</td>");
      >
      > is a syntax error because of the quotation marks.
      > Any of the following will fix it:
      >
      > document.write ('<td colspan="5">Hel lo</td>');
      > document.write ("<td colspan='5'>Hel lo</td>");
      > document.write ("<td colspan=\"5\">H ello</td>");
      > document.write ('<td colspan=\'5\'>H ello</td>');
      > document.write ("<td colspan=5>Hello </td>");
      > document.write ('<td colspan=5>Hello </td>');
      >
      >
      > I recommend the first.[/color]

      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"><sc ript
      type="text/javascript">dis playCount();</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.getEl ementById && document.create TextNode) {
      var count = document.getEle mentById('count ');
      if (count && count.firstChil d && count.replaceCh ild) {
      count.replaceCh ild(
      document.create TextNode('Hello '),
      count.firstChil d
      );
      }
      }
      }
      </script>
      <table ...>
      ....
      <tr>
      <td colspan="5"><sp an id="count">Coun t 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 <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      Working...