Shrinking the width of a WIDTH=100% table

S

Swifty

I have a CGI script that builds a table with "WIDTH=100%"

There are many paths through the code, but they all required the table
to be WIDTH=100%

Now, I find that in just one of the paths, it would be better if that
WIDTH=100% had not been specified.

Is there any JavaScript that I can insert, after the <TABLE> has
already been started, which will remove the effect of the WIDTH=100%

I'm open to a CSS solution as well, but there are other tables in the
same page which must remain WIDTH=100% so it would have to be specific
to just this one table.

N.B. My environment ensures that all users will have JavaScript
enabled.
 
C

Captain Paralytic

I have a CGI script that builds a table with "WIDTH=100%"

There are many paths through the code, but they all required the table
to be WIDTH=100%

Now, I find that in just one of the paths, it would be better if that
WIDTH=100% had not been specified.

Is there any JavaScript that I can insert, after the <TABLE> has
already been started, which  will remove the effect of the WIDTH=100%

I'm open to a CSS solution as well, but there are other tables in the
same page which must remain WIDTH=100% so it would have to be specific
to just this one table.

N.B. My environment ensures that all users will have JavaScript
enabled.

Why not alter it in that one specific path through the cgi code?
 
J

Jukka K. Korpela

Is there any JavaScript that I can insert, after the <TABLE> has
already been started, which will remove the effect of the WIDTH=100%

Yes. I suppose you have considered the obvious solution of not writing
WIDTH=100% for a specific table, and I suppose the reason for rejecting
that idea is in the logic of your server-side code: you're in the midst
of writing data when you find out that something should have been done
earlier.

Using JavaScript, you could change the width property of the <table>
element. For this, you could e.g. emit an empty <span> element with an
id so that appears inside a cell of the table, then traverse the
document tree upwards until you find the <table> element:

<table width="100%" border>
<tr><td>Hello world <td>Hello again
<tr><td>Foo <td> Bar
<span id="foo"></span>
<script>
var el = document.getElementById('foo');
do {
el = el.parentNode;
} while (el.tagName != 'TABLE');
el.width = '';
</script>
</table>

(So the <span> and <script> elements are what you would need to add to
the generated markup. The identifier 'foo' can be anything as long as it
is unique within the document.)
I'm open to a CSS solution as well, but there are other tables in the
same page which must remain WIDTH=100% so it would have to be specific
to just this one table.

You cannot validly emit CSS code that would affect the table that has
been started. No style attribute can do that. A <style> element would,
but it's invalid in <body> (though browsers allow it, to the extent that
it might even be declared a feature). And to make a CSS rule apply to a
specific table, you would probably need a class attribute on it, but I
suppose it would be too late to emit one.
 
E

Erwin Moller

Yes. I suppose you have considered the obvious solution of not writing
WIDTH=100% for a specific table, and I suppose the reason for rejecting
that idea is in the logic of your server-side code: you're in the midst
of writing data when you find out that something should have been done
earlier.

Using JavaScript, you could change the width property of the <table>
element. For this, you could e.g. emit an empty <span> element with an
id so that appears inside a cell of the table, then traverse the
document tree upwards until you find the <table> element:

<table width="100%" border>
<tr><td>Hello world <td>Hello again
<tr><td>Foo <td> Bar
<span id="foo"></span>
<script>
var el = document.getElementById('foo');
do {
el = el.parentNode;
} while (el.tagName != 'TABLE');
el.width = '';
</script>
</table>

Why not simply give the table an id and use that?

Regards,
Erwin Moller
 
J

Jukka K. Korpela

On 1/20/2012 4:04 PM, Jukka K. Korpela wrote: [...]
I suppose you have considered the obvious solution of not writing
WIDTH=100% for a specific table, and I suppose the reason for rejecting
that idea is in the logic of your server-side code: you're in the midst
of writing data when you find out that something should have been done
earlier.
[...]
Why not simply give the table an id and use that?

Because you cannot do that when you have already generated the <table>
tag and a few rows.
Or, you could do that by operating in the document tree, but this would
be just
a more complicated version of doing what I suggested.
 
S

Swifty

Why not simply give the table an id and use that?

I couldn't have put that better myself. I'll explain a little further
(albeit still simplified):

At the beginning of my CGI, I create the table. There are other tables
making up the header and the footer, but the body of the table
contains only the one. THE table. After the table has started, the
code can take myriad paths, resulting in different rows. One of these
paths would look better without the WIDTH=100%

So I can easily make this something like:

<TABLE WIDTH=100% ID=Body_Table>

Presumably I need some GetElementByID call, and then I'll be all set.

I have a GetElementByID call somewhere in the pages that I look after;
I'll use that as my working example.

I've had too good a day to risk spoiling it by an utter failure in my
JavaScript, but I'll try it over the weekend.

Today:

1. My dog, Nelly, found my bluetooth earpiece that I lost yesterday,
about 800m away from where I live. http://www.swiftys.org.uk/blog

2. I found the fragment of the tooth that broke four days ago. I
thought that I must have swallowed it, and was worried that it might
do me some harm. I had swallowed it. I won't go into further details.
 
T

Thomas 'PointedEars' Lahn

Swifty said:
I have a CGI script that builds a table with "WIDTH=100%"

First of all, CGI is an obsolete technology [1]. Use server extensions or
FastCGI [2] instead.
There are many paths through the code, but they all required the table
to be WIDTH=100%

Second, do not do that. Learn and apply CSS instead [3].
Now, I find that in just one of the paths, it would be better if that
WIDTH=100% had not been specified.

Is there any JavaScript that I can insert, after the <TABLE> has
already been started, which will remove the effect of the WIDTH=100%

Since the `width' attribute has no specified default value [4], the only
proper way is to program what you would do manually: remove the attribute.

elem.removeAttribute("width");

However, if you had used CSS, you could have reset the `width' property to
its specified initial value [3, 5]:

elem.style.width = "auto";


PointedEars
___________
[1] <http://en.wikipedia.org/wiki/Common_Gateway_Interface#Drawbacks>
[2] <http://www.fastcgi.com/drupal/>
[3] <http://www.w3.org/TR/CSS21/visudet.html#propdef-width>
[4] <http://www.w3.org/TR/html4/struct/tables.html#adef-width-TABLE>
[5] <http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties>
 
S

Swifty

However, if you had used CSS, you could have reset the `width' property to
its specified initial value [3, 5]:

elem.style.width = "auto";

I'm happy to convert to CSS if that helps. It means having to make two
changes, where perhaps one change would have been sufficient. I'm
under time constraint... I have only 363 working days left in my
career.

So, as you see, CGI is an appropriate obsolete technology for me, as
both I and my skill will be obsolete next year.

I'm going for a record in the IT industry. When I joined the company
that I work for in 1976, the programming language that I use was in
the early stages of its development, and some of my suggestions were
incorporated. I've now been programming in that language for 35 years
and 26 days; effectively doing the same job throughout that time.

This, for me, is the attraction of building webpages with CGI. It
hardly matters what programming language you use.
 
E

Evertjan.

Swifty wrote on 21 jan 2012 in comp.lang.javascript:
However, if you had used CSS, you could have reset the `width'
property to its specified initial value [3, 5]:

elem.style.width = "auto";

I'm happy to convert to CSS if that helps. It means having to make two
changes, where perhaps one change would have been sufficient. I'm
under time constraint... I have only 363 working days left in my
career.

So, as you see, CGI is an appropriate obsolete technology for me, as
both I and my skill will be obsolete next year.

I'm going for a record in the IT industry. When I joined the company
that I work for in 1976, the programming language that I use was in
the early stages of its development, and some of my suggestions were
incorporated. I've now been programming in that language for 35 years
and 26 days; effectively doing the same job throughout that time.

This, for me, is the attraction of building webpages with CGI. It
hardly matters what programming language you use.

While I grand you your personal hobbies in your career,
you do not respond on the part of PE's posting you quote above.
 
T

Thomas 'PointedEars' Lahn

Swifty said:
Thomas said:
However, if you had used CSS, you could have reset the `width' property
to its specified initial value [3, 5]:

elem.style.width = "auto";

I'm happy to convert to CSS if that helps. It means having to make two
changes, where perhaps one change would have been sufficient. I'm
under time constraint... I have only 363 working days left in my
career.

Please read my posting again, and whole this time.
[snipped autobiography]

PointedEars
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
 
D

Dr J R Stockton

In comp.lang.javascript message <8nsih7pjdunj21096qub8tnr1mluk2bons@4ax.
I have a CGI script that builds a table with "WIDTH=100%"

There are many paths through the code, but they all required the table
to be WIDTH=100%

Now, I find that in just one of the paths, it would be better if that
WIDTH=100% had not been specified.

Is there any JavaScript that I can insert, after the <TABLE> has
already been started, which will remove the effect of the WIDTH=100%

I'm open to a CSS solution as well, but there are other tables in the
same page which must remain WIDTH=100% so it would have to be specific
to just this one table.

N.B. My environment ensures that all users will have JavaScript
enabled.


That suggests to me that your CGI is emitting the table piece-by-piece,
which makes it harder to change what has already been sent.

Perhaps you can instead collect the table within the CGI as a string, or
as an array of strings later joined, and send it out from CGI only when
you can no longer want to change it. You can then alter the width in
the table within the CGI, before sending.

Conceivably, your code could be clearer if the table were contained
within a div, and that of size 100% or not - or otherwise.
 
S

Swifty

Perhaps you can instead collect the table within the CGI as a string, or
as an array of strings later joined, and send it out from CGI only when
you can no longer want to change it. You can then alter the width in
the table within the CGI, before sending.

I use that technique, but for other reasons. It is best done from the
outset; retrofitting it to a working program is harder.

The other reason is performance. Most of my CGI scripts generate quite
small pages, but some of them generate pages greater than 1MB of HTML.

If I create the 1MB page using 10,000 statements like:

Say '<TABLE tag that is 100 characters>'

....then the transfer rate between the apache server and my browser is
often as little as 20KBps.

If I buffer the individual lines, then I achieve the full transfer
rate that my ADSL connection provides. However, it can take a long
time to fill this buffer, and in the meantime the page seems
unresponsive.

So, I take a compromise; I buffer the lines, but limit the buffer to
about 10KB.

I've often wondered what slows the transfer rate when I'm sending
small chunks; presumably some inefficiency in the IP protocol when
faced with small transmissions.
 
S

Swifty

Please read my posting again, and whole this time.

While I appreciate the effort you went to, I've never read anything in
its entirety in my life. I read the bits that interested me, ignored
the parts that I've already discounted (for practical reasons) and
skimmed the rest.

The largest practical reason is that I have only 363 working days
before I retire. A number of the projects that I've worked on over the
past 35 years still require my presence in order to continue
functioning properly, and fixing that will consume about 362 of my
remaining days.

So, these days, I'm only ever looking for the "quick fix".
 
T

Thomas 'PointedEars' Lahn

Swifty said:
Thomas said:
Please read my posting again, and whole this time.

[…]
So, these days, I'm only ever looking for the "quick fix".

You must be getting senile already. The quick fix is in my posting!


PointedEars
 
J

Jukka K. Korpela

2012-01-22 15:06, Thomas 'PointedEars' Lahn trolled:
You must be getting senile already.

I would rather be senile than a troll or a person who publicly insults
people for senility.
The quick fix is in my posting!

Your posting was your usual pointless lecturing on irrelevant things,
and your "quick fix" simply did not address the problem that was
presented. Almost anyone (even you) could solve the problem that you
addressed, but the problem posed was something quite different - not at
all the extremely trivial problem about which you gave a long lecture.
 
S

Swifty

You must be getting senile already. The quick fix is in my posting!

Apparently it starts at 37 (or is it 45? I don't recall exactly; it
was in the news recently), so how close are you? :)

Yes, I saw your answer, and in all probability I will use it. Thank
you.
 
S

Swifty

I saw your answer, and in all probability I will use it.

Well, the gods are smiling on me today, with the FAQ article being on
getting the element from the ID. It was a sign, so I went and
implemented this solution.
 
T

Thomas 'PointedEars' Lahn

And I do _not_ mean using CSS instead.
Apparently it starts at 37 (or is it 45? I don't recall exactly; it
was in the news recently), so how close are you? :)

Too close to those figures, I'm afraid ;-)
Yes, I saw your answer, and in all probability I will use it. Thank
you.

You are welcome.


PointedEars
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top