http://www.perlmonks.org?node_id=181570

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

I know that you can get the size of a visitor's screen using Javascript with screen.width, but I don't know enough Javascript to know if there is a way to pass this value on to Perl some how? Or better, a way to get this value with Perl itself?

A google search didn't turn up anything Perl related and a search here only returned a way to use Term::Readkey to get the screen size of a local terminal user.

My aim isn't (at this time at least) to redirect a user based on their screen size, but rather to log this data. I somehow need to get their screen size and INSERT it into their account record in Postgresql via Perl. The only solution I've found is to use Javascript to detect their screen size, redirect them to a page based on this size, then have that page populated with pre-filled FORM data that is submitted to Perl and INSERTed. There must be a more elegant way though. Either to detect the screen size with Perl or to elegantly pass the value of the Javascript screen.width to a hidden form <input> that can be submitted to Perl.

Having little experience (or interest) in Javascript, I'm not sure how I might do this.

Replies are listed 'Best First'.
Re: Get visitor's screen size.
by amphiplex (Monk) on Jul 14, 2002 at 13:24 UTC
    Hi !

    In order to pass the screens height and width via hidden form parameters, you could insert something like this into your html:
    <script TYPE="text/javascript" LANGUAGE="JavaScript"> function fill_in_screensize() { document.forms['main_form'].width.value=screen.width; document.forms['main_form'].height.value=screen.height; return true; } </script> .... .... <form name=main_form> <input type=hidden name=width> <input type=hidden name=height> <input type=submit onClick='fill_in_screensize();'> </form>
    I don't know much javascript myself, so please don't rely on the code working on all browsers.

    ---- amphiplex
Re: Get visitor's screen size.
by Chady (Priest) on Jul 14, 2002 at 13:26 UTC

    You have to know that unless the browser sends you this information somehow in the header, you can't directly retrieve it from your perl script that is ran Server-Side so I think your only bet is, besides asking the visitor for his screen resolution, to try and read it from JavaScript and do a window.location.href="http://someserver/perlscript.pl?width=" + screen.width + "&height=" + screen.height; in your JavaScript or something like that.

    Just be sure that you have a default for people who do not have JavaScript turned on, so you do not alienate them from the site.

    I hope this helps.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      You have to know that unless the browser sends you this information somehow in the header, you can't directly retrieve it from your perl

      Yeah, I didn't expect there would be. I thought perhaps there may be some undocumented ENV variable that I might not be aware of that CGI could grab -- but I doubted it.

      I'm not looking to redirect users (at this point at least) so I'm not concerned beyond gathering this data. The site is designed to be fully functional from Netscape/Opera/MSIE 3x and greater, Mozilla and WebTV. I'm planning to enhance the "higher-end" functionality and display of the site however and rather than alienate lower-end visitors by introducing code that destroys their ability to use the site or limiting higher-end visitors from a full experience by catering to the lowest common denomenator, I am going to create a version geared at 800x600 or 1024x768 and higher and another for 800x600 or 640x480 and lower (largely plain text, tables and some images).

      As far as asking visitors... I deal with thousands of visitors a week who often have a difficult time entering the correct email address and wonder where their registration confirmation went -- or worse -- think they always have to prepend their email addresses with 'www.'. Getting a screenre solution from some of them would be like trying to get a definition of quantum theory from Ted Nugent. :)

      I didn't know thta I could simply refer to the Javascript vars outside of the SCRIPT tags without problems. That makes it much simpler!

        I didn't know thta I could simply refer to the Javascript vars outside of the SCRIPT tags without problems

        hold on a minute.. that line of code in my reply is a javascript code... you cannot use it outside script tags.. it just tells your browser to redirect to a specific address, which you construct based on the screen resolution you get..


        He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

        Chady | http://chady.net/
        would be like trying to get a definition of quantum theory from Ted Nugent.
        Actually, from what I understand, the ol' WangoTango guy is pretty smart. Now, getting quantum theory from Britney Spears, on the other hand...

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

The solution I've gone with.
by Seumas (Curate) on Jul 14, 2002 at 14:55 UTC
    Here is the solution I went with. Thanks to everyone for their assistance. It is very appreciated!

    In my HTML::Template, I have a login section. If you are not logged in (ie, we can't find a session_id cookie in your browser), we send the following to your browser:
    [HTML building some tables and the top-of-page navigation] <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> function fill_in_screensize() { document.forms['login_form'].screen_width.value=screen.width; document.forms['login_form'].screen_height.value=screen.height; return true; } </SCRIPT> <FORM name=login_form ACTION=[ACTION] METHOD=[METHOD] <input type=hidden name=screen_width> <input type=hidden name=screen_height> [input fields for username and password] <input type=submit value="submit" onClick='fill_in_screensize();'> </FORM>
    Then, in my perl code, I grab the values with $query->param('screen_width'), dbh->quote it and then stuff it into the user's account record along with the other data I track (ip_address, user-agent string, etc).

    I don't believe WebTV supports Javascript but since WebTV displays at less than 544 pixels wide, I can just parse for WebTV user-agent strings to get an idea of how many usrs have a resolution under 640x480.

    Wow! The first time Javascript has served a useful purpose in all my years!
Re: Get visitor's screen size.
by valdez (Monsignor) on Jul 14, 2002 at 13:35 UTC

    There is no way to know screen size, except using javascript. So the solution is to elegantly pass the value of the Javascript screen.width to a hidden form <input> that can be submitted to Perl.

    FYI, this topic has been discussed on mod_perl mailing list at the end of March 2002.