Team LiB
Previous Section Next Section

Handling Screen Resolution Issues

Browser differences are only half the problem you'll encounter when attempting to create a universally viewable Web page. The conditions in which the browser is running, operating system and screen resolution to name a couple, also create problems. Luckily, there is a built-in JavaScript object, Screen, which will help you with this. The Screen object contains information on the display screen's size and color depth.

Screen Properties

There are several properties that are built into the Screen object in order for you to more effectively use it. Here is a complete list of their names and descriptions of each:

  • availHeight. This property returns the height of the screen in pixels. The height of any components of the operating system's interface—such as those of the Windows Taskbar—are subtracted automatically.

  • availWidth. This property returns the width of the screen in pixels. The width of any components of the operating system's interface—such as those of the Windows Taskbar—are subtracted automatically.

  • colorDepth. This property represents the color depth of a color table, if one is in use. If no color table is being used, this property is the Screen.pixelDepth property.

  • height. This property returns the height of the screen in pixels.

  • pixelDepth. This property returns the color resolution of the display screen in bits per pixel.

  • width. This property returns the width of the screen in pixels.

Even though the Screen object is available to return information about the client's screen, it is not a good idea to give the user a message informing him that he's not viewing your Web page under ideal conditions. It's your responsibility as the programmer to make your Web pages work with as many screen variations as possible.

Working with Different Screen Settings

How to get all of your content on the page and how your images will look when they are displayed are two of the most important issues you'll encounter when dealing with different screen settings.

Getting all of your content onto a page is relatively easy because HTML has built-in features that can be used to create content that fits on any resolution. The best way to make sure that all of a page's content fits in any screen resolution is to enclose the entire content in a table (which may or not have a border) and set its width property to 600 pixels.

<html>

  <head>
    <title>
      JavaScript Professional Projects - Containing Content
    </title>
  </head>
  
  <body>

    <table width="600"><tr><td> 
      <center>
        <font size=6>JavaScript Professional Projects</font><br>
        <font size=4>Chapter 10: Containing Content</font>
      </center>

      <br><br>
      <br><br>

      <code>
        If you place all your page's content into a table that has a
        defined width of 600 pixels, it will all fit into a browser
        that is being used under any screen resolution <b>and</b> it
        will look exactly the same under any of the many screen
        resolutions.
      </code>
    
  </td></tr></table> 
  </body>

</html>

Because the smallest standard screen resolution is 640×480 pixels, your Web page will fit into any browser running under any of the many screen resolutions. Not only will all your content fit on the page, but it will also look exactly the same under any condition. The only adverse side effect of using this technique occurs when the user is viewing your Web page in a browser window that is not maximized. If this is the case, some of the content of your page will be hidden, and the user will have to use the scroll bar to view it. Fortunately, with your knowledge of the window and screen objects, this side effect can easily be avoided. Here's how:

<html>

  <head>
    <title>
      JavaScript Professional Projects - Resizing Small Windows
    </title>

    <script language="JavaScript">
    <!--
      function onLoad() 
      {
        self.moveTo( 0, 0 ); 
        self.resizeTo( screen.availWidth, screen.availHeight ); 
      }
    // -->
    </script>

  </head>

  <body onLoad="JavaScript: onLoad();">
   
    <table width="600"><tr><td>
      <center>
        <font size=6>JavaScript Professional Projects</font><br>
        <font size=4>Chapter 10: Resizing Small Windows </font>
      </center>

      <br><br>
      <br><br>

      <code>
        If you place all your page's content into a table that has a
        defined width of 600 pixels, it will all fit into a browser
        that is being used under any screen resolution <b>and</b> it
        will look exactly the same under any of the many screen
        resolutions.
      </code>

  </td></tr></table>
  </body>

</html>

This example resizes the window as soon as it loads to make sure it is big enough to hold all of the horizontal content without scroll bars. As most users will become annoyed by having their windows resized unnecessarily, it's a good idea to put a check in the onLoad() function above, so that the window will only resize if it is necessary in order to show all of the content:

function onLoad()
{
  if( self.innerWidth < 600 )
  {
     self.moveTo( 0, 0 );
     self.resizeTo( screen.availWidth, screen.availHeight );
  }
}

As a last resort, you can always display a message for the user if he is using a screen resolution that you feel your content or images just cannot accommodate. Here is a modified onLoad() function again that does just that:

function onLoad()
{
  if( self.innerWidth < 600 )
  {
    self.moveTo( 0, 0 ); 
    self.resizeTo( screen.availWidth, screen.availHeight );
  }

  if( self.innerWidth < 600 )
  {
    alert( "This Web page was designed to be viewed using a" +  
            " screen resolution of 640x480 or larger." ); 
  }
}

This function attempts to resize the window, but if it's still not large enough, a message is displayed explaining the problem.

Another important thing to remember when writing a Web page for different screen resolutions is that images will appear larger on smaller resolutions. Fortunately, the width and height properties of the HTML <IMG> tag allow you to dynamically set the size of the image. Here is a typical image tag that dynamically sizes the image to the screen:


<img src="image.jpg"
      width="JavaScript: getWidth( 150 )"  
      height="JavaScript: getHeight( 200 )">

That statement, in combination with the following two functions:

function getWidth( width ) 
{
  return( width * screen.width / 1024 ); 
}
function getHeight( height )
{
 return( height * screen.height / 768 ); 
}

will dynamically resize the image so that it will look the same on any screen resolution. These two functions—getWidth() and getHeight()—assume that the image was designed for screen resolution 1024 × 768, but you can change that by changing the resolution width and height in the return statements.


Team LiB
Previous Section Next Section