yosefm has asked for the wisdom of the Perl Monks concerning the following question:

I'm building a web-based book catalog. One of the functions I have builds a form of a few hard-coded fields (I will use mappings later, but that's not important). The formatting is done by placing each field in a box with its title and <input ...> , using <table bgcolor=...> tags.

Now, I order the boxes with a table, and all goes in a 3 collumn main table, in the middle.
The problem is when the fields overflow the size of the middle column, they push the rest aside, making it bigger than the screen.

so I'm looking for a way to determine the user's screen size and build the table with a matching number of rows.

I thought of using javasript to transfer the window.width in the URL, as a parameter.

any better way?

Replies are listed 'Best First'.
(jeffa) Re: (OT) Use javascript? ahhh!!!
by jeffa (Bishop) on Jun 09, 2003 at 16:21 UTC
    Not only is this not a Perl question, it's not really a JavaScript question either. It's an HTML design question, and trying to play that game with JavaScript is nothing short of foolish. Why not simply specify exact widths for the outter columns and make the middle column variable?
    <tr> <td width="80">stuff</td> <td width="100%">stuff</td> <td width="200">stuff</td> </tr>
    Also, you might want to consider using CSS instead, check out this example

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks, but that's not exactly what I meant. The question was about avoiding Javascript, if it's at-all possible.

      About no tables, this sounds interesting but I don't know if I'll have the time to learn that.

      Thanks anyway, guys...

        That answer will help you solve the problem. Define your cell widths and the rest will follow. Use percentage widths and if you absolutely must alter your HTML based on screen size then you'll need to use JavaScript to fetch it.

        A thought crossed my mind - would allowing the viewer to change the number of columns be a possibility? If so, you could use something like the following script:
        use strict; use warnings; use CGI qw(:standard); print header,start_html,start_form, p( 'How many columns? ', popup_menu(-name=>'cols',-values=>[1..16]), ), submit,end_form,hr, ; my $cols = param('cols') || 4; my @one_d = map sprintf('%02x',$_) x 3, reverse (0..255); my @two_d = map[ @one_d[$_..$_+$cols-1] ], range(0,$#one_d,$cols) ; print p("there are $cols columns:"), table( map {Tr( map {td{bgcolor=>"#$_"},$_} @$_ )} @two_d ), end_html, ; sub range {grep!(($_-$_[0])%($_[2]||1)),$_[0]..$_[1]}

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: (OT) Use javascript? ahhh!!!
by gellyfish (Monsignor) on Jun 09, 2003 at 16:09 UTC
    This is totally not Perl but it might be possible to put a hidden field (called,say windowidth) in your form and then do:
    form.windowidth = window.width;
    in your forms OnSubmit event or somewhere else suitable. This will then be available to your CGI program. I can't think of a way of doing this with Perl unfortunately.
    /J\
    
Re: (OT) Use javascript? ahhh!!!
by cfreak (Chaplain) on Jun 09, 2003 at 18:17 UTC

    Well if you aren't worried about Netscape 4 then its real easy to fix this problem with CSS.

    1. Don't use tables! Use <div> tags instead.
    2. Assign each column an ID
    3. Set the position for each column on the id (you'll probably need position:absolute; use top and left to put it on the page where you want it)</code>
    4. Make a class that you can specify for each column. Set the width you want the columns to be (can be a percentage as well) and set the height to 'auto' and the div will grow with your content going down the page.

    There are some other tricks like setting your divs to scroll if there is too much content. Have a look at http://w3schools.org/ for some examples and other parameters.

    Update 2003-06-10: I was asked to provide an example which can be found here. The example was tested with Mozilla, Konqueror, IE 5.5 and Opera 7. The only problem I found was that Konqueror doesn't properly recognize the scroll property. Enjoy. (P.S. code on that page is free for anyone to use/modify/whatever)

    Lobster Aliens Are attacking the world!
Re: (OT) Use javascript? ahhh!!!
by yosefm (Friar) on Jun 16, 2003 at 19:21 UTC
    Thanks everyone. I finally switched to CSS, which saves me a lot of time and also allows my to create a perl templating system from zero with a single css file to be used with CGI.

    BTW - The problem with setting column width in ie5 is that microsoft of course takes the 'recomendation' part of the w3c recomendations seriously and does whatever they bloody well like to, so it only works in optimal conditions.