Shrinking the width of a WIDTH=100% table

Discussion in 'Javascript' started by Swifty, Jan 20, 2012.

  1. Swifty

    Swifty Guest

    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.
     
    Swifty, Jan 20, 2012
    #1
    1. Advertisements

  2. Why not alter it in that one specific path through the cgi code?
     
    Captain Paralytic, Jan 20, 2012
    #2
    1. Advertisements

  3. 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.)
    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.
     
    Jukka K. Korpela, Jan 20, 2012
    #3
  4. Swifty

    Erwin Moller Guest

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

    Regards,
    Erwin Moller
     
    Erwin Moller, Jan 20, 2012
    #4
  5. 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.
     
    Jukka K. Korpela, Jan 20, 2012
    #5
  6. Swifty

    Swifty Guest

    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.
     
    Swifty, Jan 20, 2012
    #6
  7. First of all, CGI is an obsolete technology [1]. Use server extensions or
    FastCGI [2] instead.
    Second, do not do that. Learn and apply CSS instead [3].
    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>
     
    Thomas 'PointedEars' Lahn, Jan 20, 2012
    #7
  8. Swifty

    Swifty Guest

    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.
     
    Swifty, Jan 21, 2012
    #8
  9. Swifty

    Evertjan. Guest

    Swifty wrote on 21 jan 2012 in comp.lang.javascript:
    While I grand you your personal hobbies in your career,
    you do not respond on the part of PE's posting you quote above.
     
    Evertjan., Jan 21, 2012
    #9
  10. Please read my posting again, and whole this time.
    Not with javascript. Nonsense propagates like wildfire in this field.
    -- Richard Cornford, comp.lang.javascript, 2011-11-14
     
    Thomas 'PointedEars' Lahn, Jan 21, 2012
    #10
  11. In comp.lang.javascript message <[email protected]

    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.
     
    Dr J R Stockton, Jan 21, 2012
    #11
  12. Swifty

    Swifty Guest

    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.
     
    Swifty, Jan 22, 2012
    #12
  13. Swifty

    Swifty Guest

    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".
     
    Swifty, Jan 22, 2012
    #13
  14. You must be getting senile already. The quick fix is in my posting!


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 22, 2012
    #14
  15. 2012-01-22 15:06, Thomas 'PointedEars' Lahn trolled:
    I would rather be senile than a troll or a person who publicly insults
    people for senility.
    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.
     
    Jukka K. Korpela, Jan 22, 2012
    #15
  16. Michael Haufe (TNO), Jan 22, 2012
    #16
  17. Swifty

    Swifty Guest

    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.
     
    Swifty, Jan 23, 2012
    #17
  18. Swifty

    Swifty Guest

    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.
     
    Swifty, Jan 23, 2012
    #18
  19. And I do _not_ mean using CSS instead.
    Too close to those figures, I'm afraid ;-)
    You are welcome.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 23, 2012
    #19
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.