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

is there a way in perl to read the clients browser/screen metrics (like using javascript) and/or setting the browsers width and height? -or- is there a way to read javascript variables into perl variables? i've heard using "forms" (javascript -> perl) is one way, but i cant find a simple "hidden" form exaample in javascript to base by...

you mean there's any easier way?

Replies are listed 'Best First'.
Re: browser metrics
by ahmad (Hermit) on Aug 16, 2010 at 03:16 UTC

    Yes, You can use JavaScript.

    Here's what I have in mind:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new; print $q->header; if ($q->param) { my $W = $q->param('w'); my $H = $q->param('h'); print "Screen resolution is: $W X $H\n"; } print <<HTML; <html> <head><title>Test Page</title> <script language="javascript"> function GetRes() { document.getElementById('w').value = screen.width; document.getElementById('h').value = screen.height; } </script> </head> <body> <form method="post" OnSubmit="GetRes();"> <input type="hidden" name="w" id="w" value=""> <input type="hidden" name="h" id="h" value=""> <input type="submit" value="Get Resolution"> </form> </body> </html> HTML

    But keep in mind that some people have Javascript disabled so you'll have to set a default W/H in case the hidden fields were empty.

    And finally you'll have to do some verifications to make sure you are actually getting a valid resolution or whatever you want to call it.

      ahmad: this looks VERY close, but does this create the perl variable first then is used by js, or is the variable created b js then is used in perl? what i'm trying to do is gather the clients browser specs, then use them in layout proportions in my webpage (written 99% perl) when i decide the banner width, how much to display on the webpage, etc depending on where the browser settings are when the client connects to the webpage. i sort of do it now, but i cant get the specs available to me in perl so i can adjust the banner width, for instance. any more explanation would be gratefull, thanx. !-- Node text goes above. Div tags should contain sig only -->

      you mean there's any easier way?

        is there a way in perl to read the clients browser/screen metrics

        The client does not normally send that data. Solutions will involve running a program on the client that causes the client to send that data to a Perl script.

        does this create the perl variable first then is used by js, or is the variable created b js then is used in perl?

        The concept of variables being shared across languages, processes and machines makes no sense.

        As for the flow, HTTP requests are always initiated by the client. If you want to pass the browser information to a Perl script on the server, you'd need to make two requests: one that gets the JS to execute, and one that has the browser information.

Re: browser metrics
by aquarium (Curate) on Aug 16, 2010 at 03:31 UTC
    Better? to design the screens with CSS, with a fluid or fixed (or mixed) consistent layout.
    the hardest line to type correctly is: stty erase ^H