473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Blue® | last post by:
I would like to call the content of content.htm (containing only HTML codes) into index.htm. This is usually done by renaming index.htm to index.shtml and use this tag: <!--#include...
2
by: Liming | last post by:
hello, I have the follwoing code <div id="detailtable" style="visibility:hidden"> <table id="detailtablehidden" width="100%" border="0"> .... </table> </div>
22
by: Alan Silver | last post by:
Hello, I am updating some old web pages, and have come across a problem. I have some tables where the opening tag looks like... <table border="1" bordercolor="#ffffff"> which gives a nice...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.