By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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.