Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Get info from browser known only to JavaScript?

by aplonis (Pilgrim)
on Mar 14, 2001 at 02:21 UTC ( [id://64239]=perlquestion: print w/replies, xml ) Need Help??

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

I want Perl to write my CSS for me via CGI, based on info known only to JavaScript. The JS folks say it's impossible. I don't believe in impossible.

So how can I get the browser to tell Perl the screen height (or width) in pixels so that I can set my BODY font size as a fraction of the screen height in pixels. The rest of my CSS will call out FONT size in percent.

Tell me it's not impossible.

Thanks,

Gan Uesli Starling
Kalamazoo MI
  • Comment on Get info from browser known only to JavaScript?

Replies are listed 'Best First'.
Re: Get info from browser known only to JavaScript?
by arturo (Vicar) on Mar 14, 2001 at 02:37 UTC

    JS runs on the client. Perl runs on the server (in this setup, anyhow). JS starts working only after Perl has done all its work generating the page to send to the browser. So, in a way, the JS folks are right.

    But what you *can* do is go through a JS-enabled page which creates a link (or, perhaps, a form submit button) that adds the JS-only info via GET or POST to the server. Alternately, use JS to set a cookie on the passthru page and have your Perl scripts read the cookie. Subsequent Perl-generated pages can act on this information.

    Limitation: need for a passthru page, and if the user resizes her browser after the information is passed, she'll be stuck with your CSS-defined values.

    sample code:

    <script type='text/javascript'> <!-- // I don't know which property we're talking about // so this is schematic document.writeln('<input type="hidden" name="xsize" value=window.x +size>'); //blah blah // --> <input type="submit" value="Enter the Site"> </form>

    HTH, although I in no way endorse your obsession with controlling presentation to this degree =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Get info from browser known only to JavaScript?
by Masem (Monsignor) on Mar 14, 2001 at 03:20 UTC
    stepping into HTML Purist Monk Mode
    Please Please Please consider any and all alternatives before you start playing around with this... People have set their browser size and fonts to match what they prefer; for some, this might mean they are at 1024x768, with 6 pt fonts and 100 lines on the screen, for others, this is 640x480 with 24pt fonts and only 10 lines on the screen. This is usually done for readability purposes on their part. What you are suggesting that you'd be doing is forcing the font size to be a certain size so that you can specify exactly how many lines should be visible. This will annoy the former user above, and will be impossible to read by the latter one. It is usually a bad thing to play with the BODY font properties anyway since this is 99% of the test on a web site, and that is what a person will have set their fonts to read by.

    In addition, the FONT tag is depriciated. Since you are using CSS, you should be setting styles and using SPAN and DIV to set the font styles for given areas. And it is best to stick to the relative font sizes in CSS (big, bigger, small, smaller) than to even use percentages much less pixel sizes.

    HTML purist rant off


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Thank you for your kind reply.

      Actually I had this working for MSIE5 and NC4.7, in CSS and JavaScript, but now with NC6, it crashes badly. I was looking to PERL CGI as a way to escape from the multitudinous JS/Browser versions. You may visit my site at om-ah-hum.com to see what I had done with it.

      I was setting the lines per screen in pixels, so that everybody got the same not different aspect in their window. It worked pretty well for 600, 800 and 1024 size displays. The only big hitch was my jpeg background which tiled horizontally at 1280...until I widened it.

      I had set all the frames to percent of the screen, and the fontsizes to percent of the screen. You could shrink or expand the window and it looked okay. I was really quite happy with it until NC6. I did that with all three of my sites.

      Thank you,
      Gan Starling
      Kalamazoo MI
Re: Get info from browser known only to JavaScript?
by Hot Pastrami (Monk) on Mar 14, 2001 at 02:43 UTC
    It's not perfect, but here's one way:

    Have a lead-in page in which Javascript gathers the information you need. Assign the values which Javascript collects into a query string, and submit it to your CGI.

    Something like this (untested):

    <body onload="location.href = 'script.cgi?height=' + screen.availHeight">

    Using "location.href = ..." prevents the lead-in page from appearing if the user pushes the BACK button, it will skip back to the page previous to the lead-in. The weakness with this arrangement is that you'll have an obvious page transfer, but it's the only means to do it.

    I was pretty slow typing this, so it's probably already been answered a couple of times in the meantime. Oh well.

    Hot Pastrami

    Update: Heh, I guess it won't show up if I just type a "<body>" tag in my post, I gotta use &gt; and &lt;. Heh heh.
Re: Get info from browser known only to JavaScript?
by BooK (Curate) on Mar 14, 2001 at 02:49 UTC

    You could try something along those lines:

    <html> <head> <script language="JavaScript"> <!-- function fetch_size () { document.myForm.x = /* some useful code */; document.myForm.y = /* some useful code */; document.myForm.submit(); } --> <body onload="javascript:fetch_size()"> <form action="/index.php" method="post" name="myForm"> <input type="hidden" name="x" value="-1"> <input type="hidden" name="y" value="-1"> </form> </body> </html>

    Please remember that I don't know JavaScript, so this might not even compil^Wwork...

Re: Get info from browser known only to JavaScript?
by Coyote (Deacon) on Mar 14, 2001 at 02:31 UTC
    This would be a bit of a kluge, but you could have javascript gather the information you need during the body.onLoad() event, store the information you need in a cookie or as part of a URL, redirect the browser to a CGI script and gather the information from the cookie or the URL from your CGI. Hope that helps.

    ----
    Coyote

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://64239]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2025-07-15 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.