JS in tables

  • Thread starter U. Cortez - Research
  • Start date
U

U. Cortez - Research

I'm putting together some javascript code whose output needs to be
formatted into two columns. I was trying to put it in an html table
until someone alerted me that IE doesn't accept JS within tables. So
I'm looking for workarounds or alternatives. I've Googled and have
found plenty of similar questions, but no concrete solutions.

Someone recommended just putting the table tags (table, tr, td) in
document.write() functions, but this still yields the "Operation
Aborted" error in IE.

Any clues?

-U.
 
R

RobG

U. Cortez - Research said on 07/04/2006 5:24 AM AEST:
I'm putting together some javascript code whose output needs to be
formatted into two columns. I was trying to put it in an html table
until someone alerted me that IE doesn't accept JS within tables. So

IE does 'accept JS in a table'. There are two table-related script
issues with IE:

1. You can't use innerHTML to modify the internal structure

2. If createElement is used to create TR elements, they must
be appended to a tbody element, not the table element.

Other than that, it's business as usual. To modify a table, use DOM
methods. Here's a link to the Mozilla DOM reference for tables:

<URL:http://developer.mozilla.org/en/docs/DOM:table>


If you use insertRow and insertCell to modify your table, you avoid both
the above issues and you should have no problems with modern browsers
(anything after Navigator/IE 4).

Use feature detection to prevent errors in unsupported UAs.


[...]
Someone recommended just putting the table tags (table, tr, td) in
document.write() functions, but this still yields the "Operation
Aborted" error in IE.

You can use document.write to create all or part of a table while the
page loads, but that is not a good idea in general.

e.g.

<table>
<tr><td>top cell</td></tr>
<script type='text/javascript'>
document.write('<tr><td>bottom cell</td></tr>');
</script>
</table>


Using document.write after the page has finished loading will cause the
entire page to be re-written.
 
R

Richard Cornford

RobG wrote:
You can use document.write to create all or part of a
table while the page loads, but that is not a good idea
in general.

e.g.

<table>
<tr><td>top cell</td></tr>
<script type='text/javascript'>
document.write('<tr><td>bottom cell</td></tr>');
</script>
</table>
<snip>

While in reality you can write this code you are opening yourself up to
the vagaries of tag-soup HTML parsing and error correction as it is not
valid HTML. A SCRIPT element may not be a child of a TABLE element, or
the implied TBODY element, so the parser may error-correct the HTML to
imply the closing of the TABLE and TBODY when it encounters the SCRIPT
opening tag and then imply the opening of a new TABLE and TBODY when it
encounters the document written opening TR tag. Then again it may make
the whole lot into a single table. The variable and unpredictable
outcome will likely make the scripted interacting with the resulting
table(s) harder than it needs to be.

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 6 Apr 2006 12:24:35 remote, seen in
U. Cortez - Research
I'm putting together some javascript code whose output needs to be
formatted into two columns. I was trying to put it in an html table
until someone alerted me that IE doesn't accept JS within tables. So
I'm looking for workarounds or alternatives. I've Googled and have
found plenty of similar questions, but no concrete solutions.

Someone recommended just putting the table tags (table, tr, td) in
document.write() functions, but this still yields the "Operation
Aborted" error in IE.

Any clues?

Without seeing a minimal demonstration of your actual code, or knowing
which version of IE you are testing with, it's hard to know why it does
not succeed. The following works for me as an HTML page :-

<script>
var Yr=1600, X=4, St
B = "<\/th>\n<\/tr>\n<tr>\n<th>", C = "<\/th>\n<th>"
St = "<table summary=\"T1\" border=1>\n" +
"<caption>TABLE I<\/caption>\n<tr>\n<th>" +
"6543210".split("").join(C) + B +
"BCDEFGA".split("").join(C) + B +
" ".split(" ").join(" <\/th>\n<th>") + " <\/th>\n"
while (Yr<=8500) {
St += "<td>" + Yr ; Yr += 100
if (Yr%400==0) { St += "<br>" + Yr ; Yr += 100 }
St += "<\/td>\n"
X++ ; if (X==7) { X=0 ; St += "<\/tr>\n<tr>\n" } }
St += "<td>&amp;c.<\/td>\n<td>" +
" ".split(" ").join(" <\/td>\n<td>") +
" <\/td>\n<\/tr>\n<\/table>"
document.writeln(St)
</script>

However, it's certainly possible to amend an existing Table after
loading.


Read the newsgroup FAQ.
 
U

U. Cortez - Research

Ok, to be more specific, I'm trying to insert a Google Map inside one
cell of my table. However, if I do something like:

<table>
<tr><td>
<script type="text/javascript">
//google map code
</script>
</td></tr>
</table>

I get, "Internet Explorer cannot open the Internet site <location>.
Operation aborted"

-U.
 
U

U. Cortez - Research

A SCRIPT element may not be a child of a TABLE element, or
the implied TBODY element

Therein lies my problem. I need the output of the script to be
displayed within the table. How can I work around this limitation in
IE?

-U.
 
E

Evertjan.

U. Cortez - Research wrote on 07 apr 2006 in comp.lang.javascript:
Therein lies my problem. I need the output of the script to be
displayed within the table. How can I work around this limitation in
IE?

Use the DOM.

Or wrie your whole HTML as a string to be document.written.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top