Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > Table containing Javascript to build cells

Reply
Thread Tools

Table containing Javascript to build cells

 
 
P E Schoen
Guest
Posts: n/a
 
      01-12-2011
I have a table which is filled with name, title, email, and phone numbers.
To make it easier to maintain I create a script which adds the rows and
items from an array. It works fine but according to the HTML 4.01
transitional validator the script is not allowed there.

<div align="center"><h1 style="font-size:
12pt;">Contacts</h1></div>
<table cellpadding="2" cellspacing="2" border="2"
style="text-align: left; width: 342px; ">
<tbody>
<tr>
<td width="231" style="vertical-align: top;"><b>Name</b>
</td>
<td width="89" style="vertical-align: top;"><b>Phone
Number</b><br>
</td>
</tr>
<script language="JavaScript" src="contacts.js"
type="text/javascript"> </script>
</tbody>
</table>

The script is:

var contact = new Array();

contact[0] = ["Name0", "Title0", "(E-Mail Removed)",
"000-555-5555"];
...
contact[17]

for (i=0; i<=17; i++) {
document.write( "<tr><td ><p><b>" + contact[i][0] + "</b><Br>\n");
document.write( contact[i][1] + "<br>\n");
document.write( "<a href='" + contact[i][2] + "'>Email</a></td>\n");
document.write( "<td style='vertical-align: top;'><br>\n");
document.write( contact[i][3] + "<br> </td> </tr>\n");
}

I can probably construct the entire table with the script. I did not see any
information about script not allowed within a table. I referred to
http://www.w3.org/TR/html4/interact/scripts.html

Thanks,

Paul

 
Reply With Quote
 
 
 
 
dorayme
Guest
Posts: n/a
 
      01-12-2011
In article <z67Xo.93137$(E-Mail Removed)>,
"P E Schoen" <(E-Mail Removed)> wrote:

> I have a table which is filled with name, title, email, and phone numbers.
> To make it easier to maintain I create a script which adds the rows and
> items from an array. It works fine but according to the HTML 4.01
> transitional validator the script is not allowed there.
>
> <div align="center"><h1 style="font-size:
> 12pt;">Contacts</h1></div>
> <table cellpadding="2" cellspacing="2" border="2"
> style="text-align: left; width: 342px; ">
> <tbody>
> <tr>
> <td width="231" style="vertical-align: top;"><b>Name</b>
> </td>
> <td width="89" style="vertical-align: top;"><b>Phone
> Number</b><br>
> </td>
> </tr>
> <script language="JavaScript" src="contacts.js"
> type="text/javascript"> </script>
> </tbody>
> </table>


Why not move it, if not to the head of the document, you could
put it in one of the cells.

What is the point of the div surrounding the heading? Why not
style the heading directly since you are already applying a style
to it.

And is there any point in using transitional rather then strict?
Best to use em for font units, pts are more suited to print
material.

If you specify element widths in px, either it will be ignored,
or else large size user text will spill out. In the case of
tables, you are safe!

--
dorayme
 
Reply With Quote
 
 
 
 
Jukka K. Korpela
Guest
Posts: n/a
 
      01-12-2011
P E Schoen wrote:

> I have a table which is filled with name, title, email, and phone
> numbers. To make it easier to maintain I create a script which adds
> the rows and items from an array.


Such things are usually better done with server-side scripting than with
client-side JavaScript.

> </tr>
> <script language="JavaScript" src="contacts.js"
> type="text/javascript"> </script>
> </tbody>
> </table>


You have placed the <script> element between the </tr> and </tbody> tags.
That's not allowed, since it would make the <script> a child of <tbody>, and
a <tbody> cannot have any other children but <tr> elements.

Usually you could either move the <script> element at the end of the last
<td> element or, better, after the <table> element. Depending on the
scripting logic, it might be best to move the <script> element even to the
<head> part of the document (usually the "cleanest" place for <script>
elements that contain variables and functions only) or at the very end of
the body, right before the </body> tag.

But, again, depending on scripting logic, you might need to do something
else.

> The script is:
>
> var contact = new Array();
>
> contact[0] = ["Name0", "Title0", "(E-Mail Removed)",
> "000-555-5555"];


As an aside, the address is wrong, since the domain .null does not currently
exist. To indicate that an address-looking construct is just a dummy
example, use the .example or .example.com pseudo-domain. To use an
address-looking construct that detectable as invalid (e.g. for use as an
initial value that is expected to be overridden), use the .invalid
pseudo-domain.

> document.write( "<tr><td ><p><b>" + contact[i][0] +


Here we go... using document.write() makes the <script> non-movable - this
is one of the many reasons for avoiding document.write().

With the current logic, there's not much to be done except accept
non-validity. It's after all a syntactic matter only, though it is
imagenable that some future browser might discard a <script> element that
appears in a place where the syntax does not allow it - though this is
highly unlikely.

A better logic would be to create the new elements using createElement() and
add them to the DOM tree with appendChild(). That way, the <script> element
could be placed at the end of the document. (It can't be in <head> because
it needs to access an existing element to find a place where it adds the
elements, and for this, you need to ensure that the document has loaded
before the script starts running.)

> I can probably construct the entire table with the script.


Certainly. And this would another way to fix the problem.

> I did not
> see any information about script not allowed within a table. I
> referred to http://www.w3.org/TR/html4/interact/scripts.html


It's not there, because the recommendation specifies what the allowed
content of an element is, and such rules only implicitly defined the allowed
contexts of an element (i.e. where an element may appear). The
www.htmlhelp.com site contains a nice HTML 4 reference where the context
rules have been explicitly written out, see e.g.
http://www.htmlhelp.com/reference/ht...al/script.html

--
Yucca, http://www.cs.tut.fi/~jkorpela/

 
Reply With Quote
 
P E Schoen
Guest
Posts: n/a
 
      01-12-2011
"Jukka K. Korpela" wrote in message
news:_RcXo.4761$(E-Mail Removed)...

> P E Schoen wrote:


>> I have a table which is filled with name, title, email, and phone
>> numbers. To make it easier to maintain I create a script which adds
>> the rows and items from an array.


> Such things are usually better done with server-side scripting than
> with client-side JavaScript.


>> </tr>
>> <script language="JavaScript" src="contacts.js"
>> type="text/javascript"> </script>
>> </tbody>
>> </table>


> You have placed the <script> element between the </tr> and </tbody>
> tags. That's not allowed, since it would make the <script> a child of
> <tbody>, and a <tbody> cannot have any other children but <tr>
> elements.


Using the http://www.htmlhelp.com/reference/ht...al/script.html it
seems that script can be placed within any block elements. I tried
commenting out the <tbody> tags and I also tried a <div> block around the
script but failed validation. I tried commenting the script and it does not
run.

These also seem useful: http://www.html-tags-guide.com/html-tbody-tag.html
http://javascript.about.com/library/blheadbody.htm

[snip]

>> The script is:
>>
>> var contact = new Array();
>>
>> contact[0] = ["Name0", "Title0", "(E-Mail Removed)",
>> "000-555-5555"];


> As an aside, the address is wrong, since the domain .null does
> not currently exist. To indicate that an address-looking construct
> is just a dummy example, use the .example or .example.com
> pseudo-domain. To use an address-looking construct that detectable
> as invalid (e.g. for use as an initial value that is expected to be
> overridden), use the .invalid pseudo-domain.


Yes, I just replaced actual data with dummies for this post.

>> document.write( "<tr><td ><p><b>" + contact[i][0] +


> Here we go... using document.write() makes the <script>
> non-movable - this is one of the many reasons for avoiding
> document.write().


> With the current logic, there's not much to be done except
> accept non-validity.


> A better logic would be to create the new elements using
> createElement() and add them to the DOM tree with
> appendChild(). That way, the <script> element could be
> placed at the end of the document. (It can't be in <head>
> because it needs to access an existing element to find a
> place where it adds the elements, and for this, you need
> to ensure that the document has loaded before the script starts running.)


Maybe something like this:
http://www.webmasterworld.com/javascript/3068770.htm

>> I can probably construct the entire table with the script.


> Certainly. And this would another way to fix the problem.


That probably makes most sense. It would encapsulate the entire table in a
single script.

Although it might be better to ask in a separate thread or on AWW, what I am
trying to do is build a new website for the local Sierras Club group. The
original style was: http://maryland.sierraclub.org/baltimore.

I have been working on this as a replacement:
http://www.pauleschoen.com/SierraClu...indexnew.shtml

It is modeled after another local website:
http://maryland.sierraclub.org/Western-MD and it as well as several other
local sites are modeled on the Maryland Chapter website:
http://maryland.sierraclub.org.

But many people in our group like this one:
http://maryland.sierraclub.org/Montgomery. It does look slick, and it is
also one of the few that actually pass validation. But I think the one I
chose will be easier (for me) to maintain, and it may not involve copyright
issues. I am also trying to set up a system where members can submit
content. I have a form on the websites that uses a perl script and SQLite.

The Excom may be willing to hire a professional website designer for several
thousand dollars to make a very attractive site that hopefully will draw
more people to use it. But I have a hit counter on my site and it has
averaged only 10 per day, most of which have been my own accesses and search
engines and such. So the question is, "If we build it, will they come?" I'd
rather have them spend the money for other projects, and I'm doing this as a
volunteer. But I'm not a website professional and I have only been dabbling
with HTML, Javascript, and CGI. So, if anyone is interested in tackling this
project, let me know, and we might be able to use your services either as a
tax-deductible donation, or for hire.

Thanks,

Paul
www.pstech-inc.com

 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      01-12-2011
P E Schoen wrote:

> Using the
> http://www.htmlhelp.com/reference/ht...al/script.html it seems
> that script can be placed within any block elements.


Yes. Which elements are block elements?

> I tried commenting out the <tbody> tags


Why did you expect that to help?

> and I also tried a <div> block around
> the script but failed validation.


Why did you expect that to help? In a context where only <tr> elements are
allowed, a <div> wrapper cannot possibly help.

> These also seem useful:
> http://www.html-tags-guide.com/html-tbody-tag.html
> http://javascript.about.com/library/blheadbody.htm


Ummm... did they actually help you?

> Yes, I just replaced actual data with dummies for this post.


No, as I wrote, .null is wrong even for dummy data.

> Maybe something like this:
> http://www.webmasterworld.com/javascript/3068770.htm


Maybe what? URLing is like burping, just more annoying.

Sorry, I lost my interest in the problem, even without seeing what the real
problem was.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      01-17-2011
Ben C wrote:

> Can't you put the script in the <head> but write it like this:
>
> function main()
> {
> // do everything ...
> }
>
> window.onload = main;
>
> That's how I usually do it. The script only runs once all the document
> is there.


Yes, that's a feasible approach too. Putting the script at the end of <body>
just sounds a bit more natural to me - the document then starts with the
document proper (content with HTML markup) and ends with associated script
code that is supposed to operate on the document.

It would actually make sense to put any <style> elements at the end as well.
But although this works on most browsers, it violates formal HTML syntax,
and it also generates "flash of unstyled content" for obvious reasons.

Both scripts and stylesheets are usually best placed in external files and
just referenced in the HTML document. It may appear to be simpler (and more
efficient) to embed them, especially if they are very short, but very often
the amount of code grows unexpectedly and you find yourself with gross
amounts of embedded code.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

 
Reply With Quote
 
Eric Bednarz
Guest
Posts: n/a
 
      01-27-2011
"Jukka K. Korpela" <(E-Mail Removed)> writes:

> […] Putting the script at the end of
> <body> just sounds a bit more natural to me - the document then starts
> with the document proper (content with HTML markup) and ends with
> associated script code that is supposed to operate on the document.


The problem with this approach is that you don’t really know if all host
object properties already have the values (yet) that you want.
E.g. WebKit-based browsers can have problems to retrieve correct
horizontal offset(Width|Left) values of element nodes that are already
accessible in the DOM; this appears to depend on race conditions and is
a bitch to debug (network latency can do a very good job hiding this).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert yyyymmdd text cells into date cells =?Utf-8?B?ZF9jYW1wZWxvQGhvdG1haWwuY29t?= Microsoft Certification 2 11-22-2006 04:52 PM
Writing to Cells in the Detailsview Insertmode makes cells uneditable. SixStringSlaya@gmail.com ASP .Net 0 02-28-2006 10:23 PM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM
Cells[].Text or Cells[].Controls[0] Joel Finkel ASP .Net Datagrid Control 0 09-01-2003 04:42 PM
Re: width of table cells, table inside of form bbxrider HTML 0 07-14-2003 08:02 PM



Advertisments