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

B

bissatch

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
 
R

Random

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.
 
G

Grant Wagner

Random said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top