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

I wonder if with perl is possible to detect user screen resolution? Any suggestion besides using JavaScript for it??? I need to use perl because a need the variable to do a check based on the user screen resolution. Thanks for the help!!!!
edited by boo_radley : title change (was : Resolution)
  • Comment on Detecting web clients monitor resolution without javascript

Replies are listed 'Best First'.
Re: Resolution
by theorbtwo (Prior) on Jan 15, 2003 at 05:20 UTC

    As with most things, context is key. If you're trying to read the screen resolution of the reader's computer from a CGI script, you can't do it directly. Since you specified no JavaScript, all that's left to suggest is VBScript or some sort of custom control. CGIs don't magicly have some sort of access to the users' computer that HTML pages don't. All they can do is read the headers and form data sent to them.

    If you're using some sort of GUI toolkit to make a program that runs localy, the answer depends on the toolkit. I don't happen to know the answer for any of them, but a search through the documentation of the module in question will probably reval somthing. If you're writing a program designed for win32, try Win32::API, and a search on MSDN for "screen resolution". The search will reveal a page called "C++ Q&A: Stopping Screen Savers, Detecting Screen Resolution, Adding Status Bar Buttons -- MSDN Magazine, December 2001", which will point you at the documentation for GetSystemMetrics, which should be fairly easy to interface to with Win32::API, assuming you have a copy of windows.h handy. (If you don't, reply to this node, and let me know what constants you want. windows.h will be included with any C compiler for win32. Search for it online first, though, because you might be able to a copy.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Resolution
by waswas-fng (Curate) on Jan 15, 2003 at 05:06 UTC
    You can't really. One way to do something like this is to use JavaScript to set a hidden field on a login page or some such that pushes that ingo bhack to your CGI. the issue is that a user can change his window size anytime. Why not just write HTML that looks good no matter what size the window is?

    -Waswas
      Javascript is really the best way i've seen to do this. If you absolutely have to have the current browser dimensions, then there is a way I can see that you can do this.

      What you would have to do is make the perl script display a page with the a subroutine which reloads the script, but passes the page dimensions. The script would then load the actual page. To handle browser resizing, you would then have the page reload itself everytime the browser was resized (using the onresize javascript function i.e. <BODY onresize="onResize();"> ) Your javascript function just needs to feed the new size details back to the perl script.

      Hope that makes sense - not elegant, but it would be a way of doing it.
      of course you can if you use device size dependant css rules and import an url e.g. in form of an 1px gif dummy that submits the width / height as get parameters. of course that would cause a big css file that you would probably generate using a script, but it *is* possible to track the dimension without any javascript in pure css and submit it back to the server.

        This is a 13 year old thread. Better to start your reply with, "Today you could try this monstrous hack that wouldn't work on some browsers and might need a couple dozen subrules to account for view-port resolution and browser behavior" instead of, "Of course you can." :P

Re: Detecting web clients monitor resolution without javascript
by dws (Chancellor) on Jan 15, 2003 at 08:05 UTC
    I wonder if with perl is possible to detect user screen resolution?

    While this isn't possible in general, earlier today I happened across an exception while playing with a friend's Danger Hiptop. The Hiptop, through the Danger proxy server, sends Http-X-AvantGo-Screensize. The contents are the Base64-encoded screen dimension.

    use MIME::Base64; print decode_base64($ENV{'HTTP_X_AVANTGO_SCREENSIZE'});
    prints
    240x320 P
    I have no idea what the 'P' is. A google search turned up no useful info.

Re: Detecting web clients monitor resolution without javascript
by LAI (Hermit) on Jan 15, 2003 at 14:32 UTC

    Without Javascript? Well, I'd probably go with something like waswas-fng's suggestion, but the only non-JS solution I can think of (no, VBScript is not a solution to anything :o) is:

    <form action="scr_res.pl" method="POST"> Please enter your screen resolution:<br /> Width (px): <input type="text" name="res_x" /> Height (px): <input type="text" name="res_y" /><br /> <input type="submit" value="Submit" /> </form>

    LAI
    :eof
Re: Detecting web clients monitor resolution without javascript
by Cody Pendant (Prior) on Jan 15, 2003 at 09:16 UTC
    One way to do this, which uses JavaScript, but only once, is with a cookie. Read the resolution with JS, set a cookie with JS, after that, read it with Perl. You can probably do it with a Java Applet too.

    But, like everyone else said, there's no foolproof way to do it and you'll only get yourself in trouble by trying. You need to re-examine the philosophy that says that knowing the res is the only fix for your problem.
    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.” M-J D

Re: Detecting web clients monitor resolution without javascript
by peregrine (Acolyte) on Jan 15, 2003 at 14:44 UTC
    Another thought just came to me - are you interested in screen size or browser window size, as they may not be the same thing. Not everyone uses their browser at full screen.

    You're still basically looking at a javascript/perl solution no matter what, it just changes which variables you use to calculate the size with.