471,311 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 software developers and data experts.

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">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

Jul 23 '05 #1
2 5480
bi******@yahoo.co.uk 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.

Jul 23 '05 #2
"Random" <ra*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
bi******@yahoo.co.uk 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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Liming | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.