faqts : Computers : Programming : Languages : PHP : Not Quite PHP : Javascript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

155 of 201 people (77%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Can PHP determine the browser window size?
How can I determine the size of the browser in Javascript?
How can I get the color depth that the client is using?

Jun 30th, 1999 19:56
Nathan Wallace,


PHP is run on the server side.  It does not know any information about
the client unless the information is sent to the server.

PHP can determine the browser type since most browsers send this
information along with the request to the server.

You will need to use something like Javascript to determine the browser
window size.  There are a number of solutions presented below:

Solution 1:

This code will get the available width of the browser window (ie: the
area that is usable by web pages)

    <Script>
    if (navigator.appName == 'Netscape' && document.layers != null) {
        wid = window.innerWidth;
        hit = window.innerHeight;
    }
    if (document.all != null){
        wid = document.body.clientWidth;
        hit = document.body.clientHeight;
    }
    document.write('Height '+hit+', Width '+wid);
    </script>

You might like to use this rather than the screen dimensions, since the
user might not have his/her browser maximized.  This works on NS4+ and
IE4+, but hasn't been tested it with earlier versions.

Solution 2:

This one gets the screen information from the Java AWT Toolkit.  This
should work on all 3.x Java enabled browsers and above (assuming the
user has Java & JavaScript enabled).

<!--script language="JavaScript">
   var tool = java.awt.Toolkit;
   var size = new         
         java.awt.Dimension(tool.getDefaultToolkit().getScreenSize());
   var myWidth = size.width;
   var myHeight = size.height;
   if (myWidth < 800) {
      alert('Your screen resolution is set to ' + myWidth+'x'+myHeight); 
</script-->

Instead of alerting the user to their screen resolution, just pass
myWIdth and myHeight to a PHP script.

Solution 3:

    <script language="javascript1.2"><!--
    rz="na";                 // Screen Rez
    cd="na";                 // Color Depth
    bn=navigator.appName;    // Browser Name
    if (bn.substring(0,9)=="Microsoft") {
       bn="MSIE";            // browser name, cont'd
    };         
    rz=screen.width+"*"+screen.height;  // Screen Rez, cont'd
    cd=(bn=="MSIE")?screen.colorDepth:screen.pixelDepth; // Color depth
    if (cd=="undefined") {
        cd="na";             // Color depth, cont'd
    }; 
    arg="<a href='output.php3?";
    arg+="bn="+bn+"&rz="+rz+"&cd="+cd;
    arg+="'>Find your screen rez</a>";
    document.write(arg);
    //--></script>

Referencing '$rz' and '$cd' in output.php3 (after the link is followed)
would then give you screen rez and color depth, respectively in your PHP
script.  Note that this example only works on browsers w/ JS 1.2 or
higher.